1. 定義服務類
<?php
declare (strict_types=1);
namespace app\api\service\common;
use think\facade\Cache;
class RedisService
{
private $expire;
private $expire_at;
public function client(): ?object
{
return Cache::store('redis')->handler();
}
private function cacheKey(string $key): string
{
return Cache::getCacheKey($key);
}
public function cache($key, callable $callback, int $expire = 3600)
{
$cache = $this->client()->get($key);
if (! $cache || ! unserialize($cache)) {
$data = $callback();
$this->client()->set($key, $cache = serialize($data), $expire);
}
return unserialize($cache);
}
public function lock($key, callable $callback, int $timeout = 10): array
{
$lock = $this->client()->get($key);
if ($lock) return ['code' => 0, 'data' => null];
$this->client()->setex($key, $timeout, 1);
$data = $callback();
$this->client()->del($key);
return ['code' => 1, 'data' => $data];
}
public function setExpire($ttl)
{
if ($this->expire_at) throw new \Exception('setExpire and setExpireAt can not set both');
$this->expire = $ttl;
return $this;
}
public function setExpireAt($timestamp)
{
if ($this->expire > 0) throw new \Exception('setExpire and setExpireAt can not set both');
$this->expire_at = $timestamp;
return $this;
}
public function __call($name, $arguments)
{
$cache_key = $this->cacheKey($arguments[0]);
$result = $this->client()->{$name}(...$arguments);
$this->expire && $this->client()->expire($cache_key, $this->expire);
$this->expire_at && $this->client()->expireAt($cache_key, $this->expire_at);
return $result;
}
}
2. 定義門面Facade
<?php
namespace app\api\facade;
use app\api\service\common\RedisService;
use think\Facade;
class Redis extends Facade
{
protected static function getFacadeClass()
{
return RedisService::class;
}
}
3. 如何使用
3.1 程式鎖
public function test()
{
$a = 1;
$b = 2;
$result = Redis::lock('lock:demo', function () use ($a, $b) {
return $a + $b;
}, 5);
if ($result['code'] == 0) return '操作頻繁,請稍後再試';
return $result['data'];
}
3.2 方法資料快取
public function test()
{
$a = 1;
$b = 2;
$result = Redis::cache('cache:demo', function () use ($a, $b) {
return $a + $b;
}, 5);
return $result;
}
3.3 簡化過期時間設定
Redis::setExpire(86400)->hSet('expire:demo', 'hash-key', 'hash-value');
Redis::setExpireAt(strtotime('2021-08-24 23:59:59'))->hSet('expireAt:demo', 'hash-key', 'hash-value');
3.4 普通呼叫
Redis::set('set:demo', 132456);
Redis::client()->set('set:demo', 132456);
本作品採用《CC 協議》,轉載必須註明作者和本文連結