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_', ]