set($name, $value, self::getExpire($expire)); } /** * 如果不存在则写入缓存 * @param string $name * @param bool $default * @return mixed * @throws \throwable */ public static function get(string $name, $default = false, int $expire = null) { return self::handler()->remember($name, $default, self::getExpire($expire)); } /** * 删除缓存 * @param string $name * @return bool * @throws \Psr\SimpleCache\InvalidArgumentException */ public static function delete(string $name) { return CacheStatic::delete($name); } /** * 缓存句柄 * * @return \think\cache\TagSet|CacheStatic */ public static function handler() { return CacheStatic::tag(self::$globalCacheName); } /** * 清空缓存池 * @return bool */ public static function clear() { return self::handler()->clear(); } /** * 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'); } } /** * 放入令牌桶 * @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; } } /** * 清除所有令牌桶 * @param string $type * @return bool */ public static function clearTokenAll(string $type = 'admin') { try { return self::redisHandler($type)->clear(); } catch (\Throwable $e) { return false; } } /** * 清除令牌桶 * @param string $type * @return bool */ public static function clearToken(string $key) { try { return self::redisHandler()->delete($key); } catch (\Throwable $e) { return false; } } /** * 查看令牌是否存在 * @param string $key * @return bool */ public static function hasToken(string $key) { try { return self::redisHandler()->has($key); } 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; } } }