/// <summary>
///缓存操作类
/// </summary>
public class Cache
{
private static List<string> _allUseCacheKey = new List<string>();
private static ObjectCache _cache = MemoryCache.Default;
/// <summary>
///添加缓存
/// </summary>
/// <param name="key">Key 唯一</param>
/// <param name="value">值</param>
/// <param name="cacheOffset">超时时间</param>
public static void Add(string key, object value, DateTimeOffset cacheOffset)
{
if (_allUseCacheKey.Contains(key))
{
Remove(key);
}
_allUseCacheKey.Add(key);
_cache.Add(key, value, cacheOffset);
}
/// <summary>
///移除缓存
/// </summary>
/// <param name="key">Key</param>
public static void Remove(string key)
{
if (_allUseCacheKey.Contains(key))
{
_allUseCacheKey.Remove(key);
}
_cache.Remove(key);
}
/// <summary>
///读取缓存
/// </summary>
/// <param name="key">Key</param>
/// <returns></returns>
public static object Read(string key)
{
if (_allUseCacheKey.Contains(key))
return _cache[key];
return null;
}
/// <summary>
///清除所有缓存
/// </summary>
public static void Clear()
{
foreach (string value in _allUseCacheKey)
{
_cache.Remove(value);
}
_allUseCacheKey.Clear();
}
}