think-cache
think-cache是從thinkphp框架抽離出的一個(gè)組件,并添加了連接池功能,自動支持協(xié)程和非協(xié)程環(huán)境。
注意
當(dāng)前手冊為 webman v2 版本,如果您使用的是webman v1版本,請查看 v1版本手冊
安裝
composer require -W webman/think-cache
安裝后需要restart重啟(reload無效)
配置文件
配置文件為 config/think-cache.php
使用
<?php
namespace app\controller;
use support\Request;
use support\think\Cache;
class UserController
{
public function db(Request $request)
{
$key = 'test_key';
Cache::set($key, rand());
return response(Cache::get($key));
}
}
提供的接口
// 設(shè)置緩存
Cache::set('val','value',600);
// 判斷緩存是否設(shè)置
Cache::has('val');
// 獲取緩存
Cache::get('val');
// 刪除緩存
Cache::delete('val');
// 清除緩存
Cache::clear();
// 讀取并刪除緩存
Cache::pull('val');
// 不存在則寫入
Cache::remember('val',10);
// 對于數(shù)值類型的緩存數(shù)據(jù)可以使用
// 緩存增+1
Cache::inc('val');
// 緩存增+5
Cache::inc('val',5);
// 緩存減1
Cache::dec('val');
// 緩存減5
Cache::dec('val',5);
// 使用緩存標(biāo)簽
Cache::tag('tag_name')->set('val','value',600);
// 刪除某個(gè)標(biāo)簽下的緩存數(shù)據(jù)
Cache::tag('tag_name')->clear();
// 支持指定多個(gè)標(biāo)簽
Cache::tag(['tag1','tag2'])->set('val2','value',600);
// 刪除多個(gè)標(biāo)簽下的緩存數(shù)據(jù)
Cache::tag(['tag1','tag2'])->clear();
// 使用多種緩存類型
$redis = Cache::store('redis');
$redis->set('var','value',600);
$redis->get('var');