cache ? Yii::$app->cache : new \yii\caching\DummyCache(); } /** * 设置缓存 * @param string $key 缓存键值 * @param string|array $value 缓存值 * @param integer|null $duration 缓存有效时长 */ public static function setCache($key, $value, $duration=null) { $duration = $duration == null ? self::getMaxAge() : $duration; $cache = self::getCacheObj(); return $cache->set($key, $value, $duration); } /** * 删除缓存 * @param string $key 缓存键值 */ public static function deleteCache($key) { $cache = self::getCacheObj(); return $cache->delete($key); } /** * 获取缓存 * @param string $key 缓存键值 */ public static function getCache($key) { $cache = self::getCacheObj(); return $cache->get($key); } /** * 随机获取缓存时间 * @param integer $min 最小整数 * @param integer $max 最大整数 * return integer */ public static function getMaxAge($min = 300, $max = 600) { return mt_rand($min,$max); } /** * 获取全局存储键 * @param string $key 缓存键值 */ public static function getGlobalKey($key) { return self::GLOBAL_KEY_PREFIX . $key; } /** * 设置全局存储 * @param string $key 缓存键值 * @param string|array $value 缓存值 * @param integer|null $duration 缓存有效时长 */ public static function setGlobal($key, $value, $duration = null) { $key = self::getGlobalKey($key); return self::setCache($key, $value, $duration); } /** * 获取全局存储值 * @param string $key 缓存键值 */ public static function getGlobal($key) { $key = self::getGlobalKey($key); return self::getCache($key); } /** * 删除全局存储 * @param string $key 缓存键值 */ public static function deleteGlobal($key) { $key = self::getGlobalKey($key); return self::deleteCache($key); } /** * 获取用户存储键 * @param string $key 缓存键值 */ public static function getUserKey($key) { $userID = Yii::$app->user->isGuest ? 0 : Yii::$app->user->id; return self::USER_KEY_PREFIX . $userID . '_' . $key; } /** * 设置用户存储 * @param string $key 缓存键值 * @param string|array $value 缓存值 * @param integer|null $duration 缓存有效时长 */ public static function setUser($key, $value, $duration = null) { $key = self::getUserKey($key); return self::setCache($key, $value, $duration); } /** * 获取用户存储 * @param string $key 缓存键值 */ public static function getUser($key) { $key = self::getUserKey($key); return self::getCache($key); } /** * 删除用户存储 * @param string $key 缓存键值 */ public static function deleteUser($key) { $key = self::getUserKey($key); return self::deleteCache($key); } /** * 获取会话存储键 * @param string $key 缓存键值 */ public static function getSessionKey($key) { $sessionID = Yii::$app->session->id; return self::SESSION_KEY_PREFIX . $sessionID . '_' . $key; } /** * 设置会话存储 * @param string $key 缓存键值 * @param string|array $value 缓存值 * @param integer|null $duration 缓存有效时长 */ public static function setSession($key, $value, $duration = null) { $key = self::getSessionKey($key); return self::setCache($key, $value, $duration); } /** * 获得会话存储 * @param string $key 缓存键值 */ public static function getSession($key) { $key = self::getSessionKey($key); return self::getCache($key); } /** * 删除会话存储 * @param string $key 缓存键值 */ public static function deleteSession($key) { $key = self::getSessionKey($key); return self::deleteCache($key); } }