using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Runtime.Caching;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace Helpers
|
{
|
/// <summary>
|
/// 公共缓存类
|
/// </summary>
|
public static class CacheHelper
|
{
|
private static ObjectCache cache = System.Runtime.Caching.MemoryCache.Default;
|
|
/// <summary>
|
/// 查询缓存是否存在
|
/// </summary>
|
/// <param name="key">缓存键</param>
|
/// <returns>是否存在</returns>
|
public static bool ExistsCache(string key)
|
{
|
return cache.Contains(key);
|
}
|
|
/// <summary>
|
/// 根据缓存键移除缓存对象
|
/// </summary>
|
/// <typeparam name="T">对象类型</typeparam>
|
/// <param name="key">缓存键</param>
|
public static void RemoveCache<T>(string key) where T : class
|
{
|
if (!cache.Contains(key)) return;
|
var model = GetCache<T>(key);
|
foreach (var a in model.GetType().GetProperties()){
|
a.SetValue(model,"");
|
}
|
SetCache<T>(key, model, 1);
|
cache.Remove(key);
|
}
|
|
/// <summary>
|
/// 设置缓存对象
|
/// </summary>
|
/// <typeparam name="T">对象类型</typeparam>
|
/// <param name="key">缓存键</param>
|
/// <param name="obj">缓存对象</param>
|
/// <param name="expires">过期时间(单位:分钟,默认值30)</param>
|
public static void SetCache<T>(string key, T obj, double expires = 30) where T : class
|
{
|
DateTimeOffset cc = DateTimeOffset.Now.AddMinutes(expires);
|
|
cache.Set(key, obj, cc);
|
}
|
|
/// <summary>
|
/// 根据缓存键获取缓存对象
|
/// </summary>
|
/// <typeparam name="T">对象类型</typeparam>
|
/// <param name="key">缓存键</param>
|
/// <returns>缓存对象</returns>
|
public static T GetCache<T>(string key) where T : class
|
{
|
if (!cache.Contains(key)) return null;
|
var item = cache.GetCacheItem(key);
|
if (item != null)
|
{
|
return item.Value as T;
|
}
|
return null;
|
}
|
|
/// <summary>
|
/// 获取缓存对象,如果不存在,则重新设置
|
/// </summary>
|
/// <typeparam name="T">对象类型</typeparam>
|
/// <param name="key">缓存键</param>
|
/// <param name="func">缓存委托</param>
|
/// <param name="expires">过期时间(单位:分钟,默认值30)</param>
|
/// <returns>缓存对象</returns>
|
public static T GetCache<T>(string key, Func<T> func, double expires = 30) where T : class
|
{
|
if (!cache.Contains(key))
|
{
|
CacheItemPolicy policy = new CacheItemPolicy()
|
{
|
AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(expires)
|
};
|
|
T obj = null;
|
|
if (func != null)
|
{
|
obj = func();
|
|
if (obj != null)
|
{
|
cache.Set(key, obj, policy);
|
}
|
}
|
|
return obj;
|
}
|
else { return cache.GetCacheItem(key).Value as T; };
|
}
|
}
|
}
|