thinkphp5 redis使用

参数参考位置:thinkphp\library\think\cache\driver

class Redis extends Driver
{
    protected $options = [
        'host'       => '127.0.0.1',
        'port'       => 6379,
        'password'   => '',
        'select'     => 0,
        'timeout'    => 0,
        'expire'     => 0,
        'persistent' => false,
        'prefix'     => '',
    ];

方式一:控制器

public function index(){
        $config = [
            'host' => '192.168.70.161',
            'port' => 6379,
            'password' => 'admin999',
            'select' => 0,
            'timeout' => 0,
            'expire' => 0,
            'persistent' => false,
            'prefix' => '',
        ];

        $Redis=new Redis($config);
        $Redis->set("test","test");
        echo $Redis->get("test");
    }

方式二:符合类型缓存(配置文件)

config.php

// 默认缓存配置
 'cache'                  => [
        // 驱动方式
        'type'   => 'File',
        // 缓存保存目录
        'path'   => CACHE_PATH,
        // 缓存前缀
        'prefix' => '',
        // 缓存有效期 0表示永久缓存
        'expire' => 0,
    ],

// 复合类型缓存
'cache' =>  [
        // 使用复合缓存类型
        'type'  =>  'complex',
        // 默认使用的缓存
        'default'   =>  [
            // 驱动方式
            'type'   => 'redis',
            // 缓存保存目录
            'path'   => CACHE_PATH,
        ],
        // 文件缓存
        'file'   =>  [
            // 驱动方式
            'type'   => 'file',
            // 设置不同的缓存保存目录
            'path'   => RUNTIME_PATH . 'file/',
        ],
        // redis缓存
        'redis'   =>  [
            // 驱动方式
            'type'   => 'redis',
            // 服务器地址
            'host'       => '192.168.70.161',
            'password' => 'admin999',
        ],
    ],
Cache::store('redis')->set('name','999');
Cache::store('file')->set('name1','999');
//链接方式三:配置文件

$this->redis = new \Redis();
$this->redis->connect('192.168.70.161',6379);
$this->redis->auth('admin999');
$len = $this->redis->lLen('redis');

// thinkphp 默认不能使用redis的push、lLen等操作,需要连接自带的redis.php

猜你喜欢

转载自blog.csdn.net/zhongjie19/article/details/81068039
今日推荐