123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- /**
- *
- * @author: xaboy<365615158@qq.com>
- * @day: 2018/01/05
- */
- namespace crmeb\services;
- use think\facade\Cache as CacheStatic;
- /**
- * crmeb 缓存类
- * Class CacheService
- * @package crmeb\services
- */
- class CacheService
- {
- /**
- * 标签名
- * @var string
- */
- protected static $globalCacheName = '_cached_1515146130';
- /**
- * 过期时间
- * @var int
- */
- protected static $expire;
- /**
- * 获取缓存过期时间
- * @param int|null $expire
- * @return int
- */
- protected static function getExpire(int $expire = null): int
- {
- if (self::$expire) {
- return (int)self::$expire;
- }
- $expire = !is_null($expire) ? $expire : SystemConfigService::get('cache_config', null, true);
- if (!is_int($expire))
- $expire = (int)$expire;
- return self::$expire = $expire;
- }
- /**
- * 写入缓存
- * @param string $name 缓存名称
- * @param mixed $value 缓存值
- * @param int $expire 缓存时间,为0读取系统缓存时间
- * @return bool
- */
- public static function set(string $name, $value, int $expire = null): bool
- {
- return self::handler()->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;
- }
- }
- }
|