* @since 1.0 */ class Cache extends Service { // 各个页面cache的配置 public $cacheConfig; // cache 总开关 protected $enable = false; // 在store config中的cache配置 protected $_cache_config; // 各个页面对应的 store cache config的key public $cacheArr = [ 'category' => 'categoryPageCache', 'product' => 'productPageCache', 'home' => 'homePageCache', 'article' => 'articlePageCache', ]; /** * 得到当前的入口对应的cache的配置信息 * */ public function getCacheConfig() { $appName = Yii::$service->helper->getAppName(); $cacheConfig = Yii::$app->store->get($appName.'_cache'); if (!$cacheConfig || !is_array($cacheConfig)) { return null; } if (isset($cacheConfig['allPageCache']) && $cacheConfig['allPageCache'] == Yii::$app->store->enable) { $this->enable = true; } $this->_cache_config = $cacheConfig; return true; } public function init() { parent::init(); $this->getCacheConfig(); } /** * @param $cacheKey | String , 具体的缓存名字,譬如 product category * @return boolean, 如果enable为true,则返回为true * 根据传递的$cacheKey,从配置中读取是否开启cache */ public function isEnable($cacheKey) { $cacheConfigKey = $this->cacheArr[$cacheKey]; if ($this->enable && isset($this->_cache_config[$cacheConfigKey]) && $this->_cache_config[$cacheConfigKey] == Yii::$app->store->enable ) { return true; } else { return false; } } /** * @param $cacheKey | String , 具体的缓存名字,譬如 product category * @return int, 如果enable为true,则返回为true * 得到$cacheKey 对应的超时时间 */ public function timeout($cacheKey) { if (isset($this->cacheConfig[$cacheKey]['timeout'])) { return $this->cacheConfig[$cacheKey]['timeout']; } else { return 0; } } /** * @param $cacheKey | String , 具体的缓存名字,譬如 product category * @return string, 如果enable为true,则返回为true */ public function disableUrlParam($cacheKey) { if (isset($this->cacheConfig[$cacheKey]['disableUrlParam'])) { return $this->cacheConfig[$cacheKey]['disableUrlParam']; } else { return ''; } } /** * @param $cacheKey | String , 具体的缓存名字,譬如 product category * @return string, 如果enable为true,则返回为true * url的参数,哪一些参数作为缓存唯一的依据,譬如p(分页的值) */ public function cacheUrlParam($cacheKey) { if (isset($this->cacheConfig[$cacheKey]['cacheUrlParam'])) { return $this->cacheConfig[$cacheKey]['cacheUrlParam']; } else { return ''; } } }