tp5.1 使用redis

如题,首先设置redis服务器连接配置,在/config/cache文件中

return [
    //缓存配置为复合类型
    'type' => 'complex',
    'default' => [
        'type' => 'file',
        'expire' => 0,
        //'缓存前缀
        'prefix' => '',
        'path' => '../runtime/cache',
    ],
    'redis' => [
        'type' => 'redis',
        'host' => '127.0.0.1',
        'port' => '6379',
        'expire' => 0,
        'prefix' => ''
    ]
];

在控制器中 use think\facade\Cache;

简单操作存取字符串

$redis = new Redis();
$redis->set('name','value');
$name = $redis->get('name');

其他如list、hash、set、sorted set使用

$redis = new Redis();
$list = $redis->handler()->lrange('list01','0','-1');

连接其他服务器的redis。注释:连接服务器的redis配置文件中要bind 0.0.0.0 我绑了操作的ip地址但是不管用,也没认真研究。当然了,如果你连得redis是从服务器的话,那么只能读,不能写。否则会报错

$option = [
            'type' => 'redis',
            'host' => '192.168.1.103',
            'port' => '6379',
            'timeout' => '0',
            'expire' => '',
            'prefix' => ''
        ];
Cache::connect($option)->set('name','value',3600);
Cache::connect($option)->handler()->rpush('k1','v1','v2','v3');

 

posted @ 2020-11-21 10:36  大尹  阅读(819)  评论(0编辑  收藏  举报