Workerman version:4.0.37
composer create-project workerman/webman
安裝ThinkCache
composer require psr/container ^1.1.1 webman/think-cache
composer -W require psr/container ^1.1.1 webman/think-orm
文件路徑:E:\lab\php\webman\webman\config\
文件路徑:E:\lab\php\webman\webman\vendor\webman\think-orm\src\ThinkOrm.php
文件改為這樣
<?php
namespace Webman\ThinkOrm;
use Webman\Bootstrap;
use Workerman\Timer;
use think\facade\Db;
use think\facade\Cache;
class ThinkOrm implements Bootstrap
{
// 進(jìn)程啟動(dòng)時(shí)調(diào)用
public static function start($worker)
{
// 配置
Db::setConfig(config('thinkorm'));
// 新增 緩存-uspear@qq.com
Db::setCache(Cache::store());
// 維持mysql心跳
if ($worker) {
Timer::add(55, function () {
$connections = config('thinkorm.connections', []);
foreach ($connections as $key => $item) {
if ($item['type'] == 'mysql') {
Db::connect($key)->query('select 1');
}
}
});
}
}
}
文件路徑:E:\lab\php\webman\webman\vendor\topthink\think-orm\src\db\PDOConnection.php
哪有這樣子玩的呀!
就是直接使用框架的緩存即可,本地已經(jīng)實(shí)現(xiàn)PSR16
thinkorm.php 配置
Db::setConfig(config('thinkorm'));
// 開(kāi)啟字段緩存
Db::setCache(\support\Cache::instance());
查詢(xún)緩存
$res = UserModel::where('id',20220005)
->cache('RestyRedis:'.UserModel::getTable())
->select();
var_dump($res->toArray());
Redis緩存結(jié)果
如果開(kāi)啟字段緩存
'fields_cache' => true,
緩存數(shù)據(jù)表結(jié)果
案例提交參考地址:https://github.com/Tinywan/webman-admin/commit/f616dededcf2dff8818e8f66ab6084722881023e
//寫(xiě)個(gè)符合psr16的緩存類(lèi)就完事
//support\Db::setCache(Container::get(RedisAdapter::class));
<?php
/**
* Created by PhpStorm.
* User: zhang
* Date: 2020/9/2
* Time: 20:47
*/
namespace support\bootstrap\db\dbcache;
use Psr\SimpleCache\CacheInterface;
use support\bootstrap\Redis;
//psr16 規(guī)范
class RedisAdapter implements CacheInterface
{
/**
* 前綴
* @var string
*/
private $pre = 'think_cache_';
/**
* @return string
*/
public function __construct($pre = '')
{
if ($pre) $this->pre = $pre;
$this->clear();
}
/**
* 生成key
* @param $key
* @return string
*/
private function addPre($key)
{
return $this->pre . $key;
}
/**
* @param string $key
* @param null $default
* @return mixed|null
*/
public function get($key, $default = null)
{
try {
$key = $this->addPre($key);
#$cache = unserialize(Redis::get($key));
$cache = json_decode(Redis::get($key), true);
if (!$cache) return $default;
return $cache;
} catch (\Throwable $th) {
return $default;
}
}
/**
* @param string $key
* @param mixed $value
* @param null $ttl
* @return bool
*/
public function set($key, $value, $ttl = null)
{
$key = $this->addPre($key);
try {
if (is_numeric($ttl)) {
#return Redis::set($key, serialize($value), 'EX', $ttl);
return Redis::set($key, json_encode($value), 'EX', $ttl);
} else {
#return Redis::set($key, serialize($value));
return Redis::set($key, json_encode($value));
}
} catch (\Throwable $th) {
return false;
}
}
/**
* @param string $key
* @return bool
*/
public function delete($key)
{
$key = $this->addPre($key);
try {
Redis::del($key);
} catch (\Throwable $th) {
return false;
}
return true;
}
/**
* @return bool|int
*/
public function clear()
{
try {
$keys = Redis::keys("{$this->pre}*");
return Redis::del($keys);
} catch (\Throwable $th) {
return false;
}
}
/**
* @param iterable $keys
* @param null $default
* @return \Generator|iterable
*/
public function getMultiple($keys, $default = null)
{
foreach ($keys as $key) {
yield $key => $this->get($key, $default);
}
}
/**
* @param iterable $values
* @param null $ttl
* @return bool
*/
public function setMultiple($values, $ttl = null)
{
foreach ($values as $key => $value) {
if (!$this->set($key, $value, $ttl)) {
return false;
}
}
return true;
}
/**
* @param iterable $keys
* @return bool
*/
public function deleteMultiple($keys)
{
foreach ($keys as $key) {
if (!$this->delete($key)) {
return false;
}
}
return true;
}
/**
* @param string $key
* @return bool
*/
public function has($key)
{
try {
$key = $this->addPre($key);
return boolval(Redis::exists($key));
} catch (\Throwable $th) {
return false;
}
}
public function __destruct()
{
$this->clear();
}
}
orm 里增加
Db::setCache(Cache::store());
tp cache 使用
topthink/think-cache 這個(gè)擴(kuò)展用 2.0.x-dev