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');

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM