tp6使用Cache::handler使用redis高级方法

884 阅读1分钟

tp6缓存使用redis高级方法

tp6的缓存提供了 set,get,inc,dec,delete 等基本方法,如果使用redis作为缓存类型,如何调用 redis 原生的那些方法呢?

1. tp6缓存

tp6内置缓存类型支持 file, redis等。

// config/cache.php

return [
    'default'    =>    'file',
    'stores'    =>    [
        // 文件缓存
        'file'   =>  [
            // 驱动方式
            'type'   => 'file',
            // 设置不同的缓存保存目录
            'path'   => '../runtime/file/',
        ],
        // redis缓存
        'redis'   =>  [
            // 驱动方式
            'type'   => 'redis',
            // 服务器地址
            'host'       => '127.0.0.1',
        ],
    ],
];

2. 使用redis作为默认缓存

return [
    'default'    =>    'redis',     // 默认缓存类型
    'stores'    =>    [
        'file'   =>  [
            'type'   => 'file',
            'path'   => '../runtime/file/',
        ],
        'redis'   =>  [
            'type'   => 'redis',
            'host'       => '127.0.0.1',
        ],
    ],
];

3.获取 redis 缓存对象

$redis = Cache::handler();

4.调用 redis 缓存对象方法

要想调用的 redis 缓存对象的方法,得需要获得的是一个什么对象。
php 的 redis 库有两个,一个是 phpredis,是一个C写的扩展库, 另一个是 predis,是一个 php 写的库。
tp6 的 Cache 类操作 redis 用的是哪个扩展呢?

tp6会优先判断是否有 phpredis 扩展,如果有就返回 phpredis 的实例,如果没有再判断是否有 predis 的扩展,有的话返回 predis的实例。如果都没有返回异常

在 vendor/topthink/framework/src/think/cache/ 目录里定义了缓存的驱动类,vendor/topthink/framework/src/think/cache/driver/Redis.php 文件定义了 redis 的驱动,在 __construct() 方法里

public function __construct(array $options = [])
{
    if (!empty($options)) {
        $this->options = array_merge($this->options, $options);
    }

    if (extension_loaded('redis')) {    // 如果有 redis 扩展
        $this->handler = new \Redis;

        if ($this->options['persistent']) {
            $this->handler->pconnect($this->options['host'], (int) $this->options['port'], (int) $this->options['timeout'], 'persistent_id_' . $this->options['select']);
        } else {
            $this->handler->connect($this->options['host'], (int) $this->options['port'], (int) $this->options['timeout']);
        }

        if ('' != $this->options['password']) {
            $this->handler->auth($this->options['password']);
        }
    } elseif (class_exists('\Predis\Client')) {     // 如果有 predis 扩展
        $params = [];
        foreach ($this->options as $key => $val) {
            if (in_array($key, ['aggregate', 'cluster', 'connections', 'exceptions', 'prefix', 'profile', 'replication', 'parameters'])) {
                $params[$key] = $val;
                unset($this->options[$key]);
            }
        }

        if ('' == $this->options['password']) {
            unset($this->options['password']);
        }

        $this->handler = new \Predis\Client($this->options, $params);

        $this->options['prefix'] = '';
    } else {
        throw new \BadFunctionCallException('not support: redis');
    }

    if (0 != $this->options['select']) {
        $this->handler->select((int) $this->options['select']);
    }
}

首先会判断 是否有 redis 的 extension 扩展,也就是 phpredis 这个C写的扩展库,如果有就是 phpredis 的实例,否则判断是否有 predis 的扩展类,如果有就是 predis 的实例,否则两个都没有就抛出异常,使用不了。
phpredis扩展: phpredis/phpredis: A PHP extension for Redis (github.com)
predis库: github.com/predis/pred…
使用 phpinfo() ,搜索 redis , 查看是否安装了 phpredis 扩展。确定好了对象实例直接去官方文档查看方法的使用就可以了。

redis.png 以下以 phpredis 举例调用hash类型的方法:

$redis = Cache::handler();
$hashKey = 'hash_key_test';
$redis->del($hashKey);
$hashData = [
    '892' => 0,
    '4407' => 0,
    '336' => 0,
];
$redis->hMset($hashKey, $hashData);
echo 'Init :<br/>';
var_dump($redis->hGetAll($hashKey));
echo '<br />';

输出:

redis2.png