123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- namespace crmeb\services;
- use think\facade\Cache as CacheStatic;
- class CacheService
- {
-
- protected static $globalCacheName = '_cached_1515146130';
-
- protected static $expire;
-
- 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;
- }
-
- public static function set(string $name, $value, int $expire = null): bool
- {
- return self::handler()->set($name, $value, self::getExpire($expire));
- }
-
- public static function get(string $name, $default = false, int $expire = null)
- {
- return self::handler()->remember($name, $default, self::getExpire($expire));
- }
-
- public static function delete(string $name)
- {
- return CacheStatic::delete($name);
- }
-
- public static function handler()
- {
- return CacheStatic::tag(self::$globalCacheName);
- }
-
- public static function clear()
- {
- return self::handler()->clear();
- }
-
- public static function redisHandler(string $type = null)
- {
- if ($type) {
- return CacheStatic::store('redis')->tag($type);
- } else {
- return CacheStatic::store('redis');
- }
- }
-
- 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;
- }
- }
-
- public static function clearTokenAll(string $type = 'admin')
- {
- try {
- return self::redisHandler($type)->clear();
- } catch (\Throwable $e) {
- return false;
- }
- }
-
- public static function clearToken(string $key)
- {
- try {
- return self::redisHandler()->delete($key);
- } catch (\Throwable $e) {
- return false;
- }
- }
-
- public static function hasToken(string $key)
- {
- try {
- return self::redisHandler()->has($key);
- } catch (\Throwable $e) {
- return false;
- }
- }
-
- public static function getTokenBucket(string $key)
- {
- try {
- return self::redisHandler()->get($key, null);
- } catch (\Throwable $e) {
- return null;
- }
- }
- }
|