缓存操作类
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using XiaoFeng;
namespace XiaoFeng.Cache
{
///
/// 缓存操作类
/// Version : V 1.0
/// Create Date : 2016-12-24
/// Author : Jacky
/// QQ : 7092734
/// Email : [email protected]
///
public class CacheHelper
{
///
/// 缓存列表
///
public static CacheData DataList
{
get { return new CacheData(); }
}
///
/// 获取数据缓存
///
/// 用于引用该对象的缓存键
/// 指定的缓存项
public static object get(string key)
{
System.Web.Caching.Cache _ = HttpRuntime.Cache;
return _[key];
}
///
/// 设置数据缓存
///
/// 用于引用该对象的缓存键
/// 要插入缓存中的对象
public static void set(string key, object value)
{
System.Web.Caching.Cache _ = HttpRuntime.Cache;
_.Insert(key, value);
}
///
/// 设置数据缓存
///
/// 用于引用该对象的缓存键
/// 要插入缓存中的对象
/// 最后一次访问所插入对象时与该对象到期时之间的时间间隔。 如果该值等效于 20 分钟,则对象在最后一次被访问 20 分钟之后将到期并被从缓存中移除。如果使用可调到期,则 absoluteExpiration 参数必须为 System.Web.Caching.Cache.NoAbsoluteExpiration。
public static void set(string key, object value, TimeSpan Timeout)
{
System.Web.Caching.Cache _ = HttpRuntime.Cache;
_.Insert(key, value, null, DateTime.MaxValue, Timeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);
}
///
/// 设置数据缓存
///
/// 用于引用该对象的缓存键
/// 要插入缓存中的对象
/// 文件路径或目录
public static void set(string key, object value, string path)
{
System.Web.Caching.Cache _ = HttpRuntime.Cache;
_.Insert(key, value, new System.Web.Caching.CacheDependency(path));
}
///
/// 设置数据缓存
///
/// 用于引用该对象的缓存键
/// 要插入缓存中的对象
/// 所插入对象将到期并被从缓存中移除的时间。 要避免可能的本地时间问题(例如从标准时间改为夏时制),请使用 System.DateTime.UtcNow,而不是 System.DateTime.Now 作为此参数值。 如果使用绝对到期,则 slidingExpiration 参数必须为 System.Web.Caching.Cache.NoSlidingExpiration。
/// 最后一次访问所插入对象时与该对象到期时之间的时间间隔。 如果该值等效于 20 分钟,则对象在最后一次被访问 20 分钟之后将到期并被从缓存中移除。如果使用可调到期,则 absoluteExpiration 参数必须为 System.Web.Caching.Cache.NoAbsoluteExpiration。
public static void set(string key, object value, DateTime absoluteExpiration, TimeSpan slidingExpiration)
{
System.Web.Caching.Cache _ = HttpRuntime.Cache;
_.Insert(key, value, null, absoluteExpiration, slidingExpiration);
}
///
/// 移除指定数据缓存
///
/// 要移除的缓存项的 System.String 标识符。
public static void remove(string key)
{
System.Web.Caching.Cache _ = HttpRuntime.Cache;
_.Remove(key);
}
///
/// 移除全部缓存
///
public static void clear()
{
System.Web.Caching.Cache _ = HttpRuntime.Cache;
IDictionaryEnumerator CacheEnum = _.GetEnumerator();
while (CacheEnum.MoveNext())
_.Remove(CacheEnum.Key.ToString());
}
}
///
/// 缓存数据操作类
///
public class CacheData
{
#region 构造器
///
/// 设置缓存数据
///
/// 键
/// 值
public CacheData(string key, object value)
{
this.add(key, value);
}
///
/// 无参构造器
///
public CacheData() { }
#endregion
#region 属性
///
/// 缓存Key值
///
private string Key
{
get
{
return System.AppDomain.CurrentDomain.SetupInformation.ConfigurationFile.ToLower() == "web.config" ? HttpContext.Current.Request.UserHostName.ToLower() : "ZW-PublicCacheKey";
}
}
///
/// 缓存数据
///
public Dictionary<string, object> data
{
get
{
var _ = CacheHelper.get(this.Key);
if (_ == null) CacheHelper.set(this.Key, new Dictionary<string, object>()); return _ == null ? new Dictionary<string, object>() : (Dictionary<string, object>)_;
}
}
#endregion
#region 方法
///
/// 添加缓存
///
/// 键
/// 值
public void add(string key, object value)
{
if (this.data.ContainsKey(key))
{
if (!this.data[key].Equals(value))
{
this.data[key] = value;
}
}
else { this.data.Add(key, value); }
}
///
/// 获取缓存值
///
/// 键
///
public object get(string key)
{
return this.data.ContainsKey(key) ? this.data[key] : null;
}
///
/// 移除
///
/// 键
public void remove(string key)
{
if(this.data.ContainsKey(key))
this.data.Remove(key);
}
///
/// 清空缓存
///
public void clear()
{
this.data.Clear();
}
#endregion
}
}