tag = $tag; $this->type = $type; $this->handler = Cache::store('file')->tag($tagLst); } public static function create($admin, $tag) { return new static($admin, $tag); } /** * 清除所以缓存 */ public static function clearAll() { Cache::store('file')->tag('__cache_' . config('app.app_key'))->clear(); } /** * 清除商户缓存 */ public static function clearMerchantAll() { Cache::store('file')->tag('__cache_mer_' . config('app.app_key'))->clear(); } /** * 清除平台缓存 */ public static function clearSystem() { Cache::store('file')->tag('__cache_sys_' . config('app.app_key'))->clear(); } /** * @param int $merId * 清除指定商户缓存 */ public static function clearMerchant($merId) { Cache::store('file')->tag('__cache_mer_' . config('app.app_key') . '_' . $merId)->clear(); } /** * 根据tag清除缓存 * @param $merId * @param $tag */ public static function clearByTag($merId, $tag) { Cache::store('file')->tag('__cache_tag_' . config('app.app_key') . '_' . $merId . '_' . $tag)->clear(); } public static function delete($key) { Cache::store('file')->delete($key); } /** * @param $key * @return string * 生成 key */ public function cacheKey($key) { if (is_array($key)) { $key = json_encode($key, JSON_UNESCAPED_UNICODE); } return '__sys_cache_' . config('app.app_key') . $this->type . $this->tag . $key; } /** * @param string|array $key * @param $cache * @param int $expire */ public function set($key, $cache, $expire = 3600) { $this->handler->set($this->cacheKey($key), $cache, $expire); } /** * @param string|array $key * @param null $default * @return mixed */ public function get($key, $default = null) { return $this->handler->get($this->cacheKey($key), $default); } /** * @param string|array $key * @return mixed */ public function has($key) { return $this->handler->has($this->cacheKey($key)); } /** * @param string|array $key * @param $value * @param int $expire * @return mixed */ public function remember($key, $value, $expire = 3600) { return $this->handler->remember($this->cacheKey($key), $value, $expire); } /** * 放入令牌桶 * @param string $key * @param array $value * @param string $type * @return bool */ public static function setTokenBucket(string $key, $value, $expire = null, string $type = 'admin') { try { $redisCahce = self::redisHandler($type); return $redisCahce->set($key, $value, $expire); } catch (\Throwable $e) { return false; } } /** * 获取token令牌桶 * @param string $key * @return mixed|null * @throws \Psr\SimpleCache\InvalidArgumentException */ public static function getTokenBucket(string $key) { try { return self::redisHandler()->get($key, null); } catch (\Throwable $e) { return null; } } /** * 查看令牌是否存在 * @param string $key * @return bool */ public static function hasToken(string $key) { try { return self::redisHandler()->has($key); } catch (\Throwable $e) { return false; } } /** * Redis缓存句柄 * * @return \think\cache\TagSet|CacheStatic */ public static function redisHandler(string $type = null) { if ($type) { return CacheStatic::store('redis')->tag($type); } else { return CacheStatic::store('redis'); } } }