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