cxfoot/components/FlashStorage.php
2023-10-27 14:25:12 +08:00

205 lines
5.4 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* @author Any
* @description KISS
* @date 2020-11-1
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\components;
use Yii;
/*
* 用于少量全局或个性化数据存储
* global存储全局数据可以在多个用户及会话间共享数据
* user可以在一个用户多个会话间共享数据
* session可以在一个会话间共享数据
*/
class FlashStorage
{
const GLOBAL_KEY_PREFIX = 'FLASH_STORAGE_GLOBAL_'; //全局数据缓存键前缀
const USER_KEY_PREFIX = 'FLASH_STORAGE_USER_'; //用户数据缓存键前缀
const SESSION_KEY_PREFIX = 'FLASH_STORAGE_SESSION_'; //会话数据缓存键前缀
/**
* 获取缓存对象
*/
public static function getCacheObj()
{
//获取cache对象如果没有配置cache对象则使用虚拟无效的cache对象DummyCache
return Yii::$app->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);
}
}