using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Text;
namespace WebService.SDK
{
public class CacheHelper
{
///
/// 默认缓存
///
private static CacheHelper Default { get { return new CacheHelper(); } }
///
/// 缓存初始化
///
private MemoryCache cache = MemoryCache.Default;
///
/// 锁
///
private object locker = new object();
///
/// 构造器
///
private CacheHelper()
{
//CacheItemPolicy policy = new CacheItemPolicy(); //创建缓存项策略
////过期时间设置,以下两种只能设置一种
//policy.AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddMinutes(5)); //设定某个时间过后将逐出缓存
//policy.SlidingExpiration = new TimeSpan(0, 0, 10); //设定某个时间段内未被访问将逐出缓存
////逐出通知,以下两种只能设置一种
//policy.UpdateCallback = arguments => { Console.WriteLine("即将逐出缓存" + arguments.Key); };
}
///
/// 从缓存中获取对象
///
/// 对象类型
/// 键
/// 缓存对象
public static object Get(string key)
{
return Default.GetFromCache(key);
}
///
/// 从缓存中获取对象
///
/// 对象类型
/// 键
/// 缓存对象
private object GetFromCache(string key)
{
lock (locker)
{
if (cache.Contains(key))
{
return cache[key];
}
return null;
}
}
///
/// 设置缓存指定时间未访问过期
///
/// 对象
/// 键
/// 数据对象
/// 过期时间
public static bool Set(string key, Object value, TimeSpan expiresIn)
{
var policy = new CacheItemPolicy()
{
SlidingExpiration = expiresIn
};
return Default.SetToCache(key, value, policy);
}
///
/// 设置缓存绝对时间过期
///
///
///
///
///
///
public static bool Set(string key, object value, DateTimeOffset expiresIn)
{
var policy = new CacheItemPolicy()
{
AbsoluteExpiration = expiresIn
};
return Default.SetToCache(key, value, policy);
}
///
/// 添加到缓存
///
/// 缓存对象类型
/// 键
/// 值
/// 结果状态
public static bool Set(string key, object value)
{
CacheItemPolicy policy = new CacheItemPolicy()
{
Priority = CacheItemPriority.NotRemovable,
};
return Default.SetToCache(key, value, policy);
}
///
/// 数据对象装箱缓存
///
/// 对象
/// 键
/// 数据对象
/// 过期时间
private bool SetToCache(string key, object value, CacheItemPolicy policy)
{
lock (locker)
{
cache.Set(key, value, policy);
return true;
}
}
///
/// 获取键的集合
///
/// 键的集合
public static ICollection Keys()
{
return Default.GetCacheKeys();
}
///
/// 获取键的集合
///
/// 键的集合
private ICollection GetCacheKeys()
{
lock (locker)
{
IEnumerable> items = cache.AsEnumerable();
return items.Select(m => m.Key).ToList();
}
}
///
/// 判断缓存中是否有此对象
///
/// 键
/// 是否存在
public static bool Contain(string key)
{
return Default.ContainKey(key);
}
///
/// 判断缓存中是否有此对象
///
/// 键
/// 是否存在
private bool ContainKey(string key)
{
lock (locker)
{
return cache.Contains(key);
}
}
///
/// 数据对象从缓存对象中移除
///
/// 键
public static bool Remove(string key)
{
return Default.RemoveFromCache(key);
}
///
/// 数据对象从缓存对象中移除
///
/// 键
private bool RemoveFromCache(string key)
{
lock (locker)
{
if (cache.Contains(key))
{
cache.Remove(key);
return true;
}
return false;
}
}
///
/// 清除实例
///
public static void Clear()
{
Default.ClearCache();
}
///
/// 清除实例
///
private void ClearCache()
{
lock (locker)
{
cache.ToList().ForEach(m => cache.Remove(m.Key));
}
}
///
/// 获取缓存对象集合
///
/// 缓存对象类型
/// 缓存对象集合
public static ICollection Values()
{
return Default.GetValues();
}
///
/// 获取缓存对象集合
///
/// 缓存对象类型
/// 缓存对象集合
private ICollection GetValues()
{
lock (locker)
{
IEnumerable> items = cache.AsEnumerable();
return items.Select(m => (T)m.Value).ToList();
}
}
///
/// 获取缓存尺寸
///
/// 缓存尺寸
public static long Size()
{
return Default.GetCacheSize();
}
///
/// 获取缓存尺寸
///
/// 缓存尺寸
private long GetCacheSize()
{
lock (locker)
{
return cache.GetCount();
}
}
}
}