TP5中的缓存使用

Thinkphp 5.0采用了 think\Cache 类来提供缓存支持

  • 缓存支持采用驱动方式,所以缓存在使用之前,需要进行连接操作,也就是缓存初始化操作。
  • 支持的缓存类型包括file、memcache、wincache、sqlite、redis和xcache。
//redis方式
$config = [
    'type'    => 'redis',
    'host'    => '127.0.0.1',
    'port'    => '6379',
    'password'=> '',
    'expire'  => '30',
    'prefix'  => 'test_',
];

$cache = Cache::connect($config);//设置缓存连接参数
$cache->set('aaa', '123456');
echo $cache->get('aaa');

//memcache方式 $config = [ 'type' => 'memcache', 'host' => '127.0.0.1', 'port' => '11211', 'expire' => '60', 'prefix' => 'test_', ]; $cache = Cache::connect($config);//设置缓存连接参数 $cache->set('aaa', '123456'); echo $cache->get('aaa');

或者通过定义配置参数的方式,在应用配置文件中添加或修改;

 'cache' => [     
    //驱动方式 'type' => 'redis', //服务器地址 'host'=>'127.0.0.1', //端口 'port'=>'6379', //密码,没有留空 'password'=>'', //过期时间,永久设0,旧版本此参数键名是timeout 'expire'=>'30', //缓存键前缀 'prefix' => 'test_', ]
posted @ 2020-03-04 14:27  不进腾讯不改名  阅读(2293)  评论(0)    收藏  举报