| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace crmeb\services;
- use think\cache\driver\Redis;
- use think\facade\Cache;
- use think\facade\Config;
- /**
- * crmeb 缓存类
- * Class CacheService
- * @package crmeb\services
- * @mixin \Redis
- */
- class CacheService
- {
- const TAG_TOPIC = 'topic';
- const TAG_CONFIG = 'config';
- const TAG_COMMUNITY = 'community';
- const TAG_BRAND = 'brand';
- const TAG_CATEGORY = 'category';
- const TAG_GROUP_DATA = 'group_data';
- const TAG_MERCHANT = 'merchant';
- protected $handler;
- protected $tag;
- protected $type;
- /**
- * @param int $admin
- * @param string $tag
- */
- public function __construct($type, $tag)
- {
- $key = config('app.app_key');
- $tagLst = ['__cache_' . $key];
- if ($type) {
- $tagLst[] = '__cache_mer_' . $key . '_' . $type;
- $tagLst[] = '__cache_mer_' . $key;
- } else {
- $tagLst[] = '__cache_sys_' . $key;
- }
- if ($tag) {
- $tagLst[] = '__cache_tag_' . $key . '_' . $type . '_' . $tag;
- }
- $this->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);
- }
- }
|