概述
public interface ICache
{
/// <summary>
/// 获取当前应用程序指定CacheKey的Cache值
/// </summary>
/// <param name="CacheKey"></param>
/// <returns></returns>
object GetCache(string CacheKey);
/// <summary>
/// 设置当前应用程序指定CacheKey的Cache值
/// </summary>
/// <param name="CacheKey"></param>
/// <param name="objObject"></param>
void SetCache(string CacheKey, object objObject);
/// <summary>
/// 设置当前应用程序指定CacheKey的Cache值
/// </summary>
/// <param name="CacheKey"></param>
/// <param name="objObject"></param>
void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration);
/// <summary>
/// 依赖缓存
/// </summary>
/// <param name="CacheKey"></param>
/// <param name="objObject"></param>
/// <param name="dep"></param>
void SetCache(string CacheKey, object objObject, CacheDependency dep);
/// <summary>
/// 清除单一键缓存
/// </summary>
/// <param name="key"></param>
void RemoveOneCache(string CacheKey);
/// <summary>
/// 清除所有缓存
/// </summary>
void RemoveAllCache();
/// <summary>
/// 以列表形式返回已存在的所有缓存
/// </summary>
/// <returns></returns>
List<string> ShowAllCache();
}
public class DataCacheDal : ICache
{
protected static volatile Cache objCache = HttpRuntime.Cache;
/// <summary>
/// 获取当前应用程序指定CacheKey的Cache值
/// </summary>
/// <param name="CacheKey"></param>
/// <returns></returns>
public object GetCache(string CacheKey)
{
return objCache[CacheKey];
}
/// <summary>
/// 设置当前应用程序指定CacheKey的Cache值
/// </summary>
/// <param name="CacheKey"></param>
/// <param name="objObject"></param>
public void SetCache(string CacheKey, object objObject)
{
objCache.Insert(CacheKey, objObject);
}
/// <summary>
/// 设置当前应用程序指定CacheKey的Cache值
/// </summary>
/// <param name="CacheKey"></param>
/// <param name="objObject"></param>
public void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
{
objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);
}
/// <summary>
/// 设置依赖缓存
/// </summary>
/// <param name="CacheKey"></param>
/// <param name="objObject"></param>
/// <param name="dep"></param>
public void SetCache(string CacheKey, object objObject, CacheDependency dep)
{
objCache.Insert(
CacheKey,
objObject,
dep,
Cache.NoAbsoluteExpiration, //从不过期
Cache.NoSlidingExpiration, //禁用可调过期
CacheItemPriority.Default,
null);
}
/// <summary>
/// 清除单一键缓存
/// </summary>
/// <param name="key"></param>
public void RemoveOneCache(string CacheKey)
{
objCache.Remove(CacheKey);
}
/// <summary>
/// 清除所有缓存
/// </summary>
public void RemoveAllCache()
{
IDictionaryEnumerator CacheEnum = objCache.GetEnumerator();
if (objCache.Count > 0)
{
ArrayList al = new ArrayList();
while (CacheEnum.MoveNext())
{
al.Add(CacheEnum.Key);
}
foreach (string key in al)
{
objCache.Remove(key);
}
}
}
/// <summary>
/// 以列表形式返回已存在的所有缓存
/// </summary>
/// <returns></returns>
public List<string> ShowAllCache()
{
List<string> cachelist = new List<string>();
if (objCache.Count > 0)
{
IDictionaryEnumerator CacheEnum = objCache.GetEnumerator();
while (CacheEnum.MoveNext())
{
cachelist.Add(CacheEnum.Key.ToString());
}
}
return cachelist;
}
}
public class DataCacheBLL
{
//可以更灵活的设置过期时间和方法抽离
private static readonly ICache icache = null;
//缓存过期时间 分钟
private readonly int intcachetime = 30;
private static object lockHelp = new object();
private static volatile DataCacheBLL instance = null;
static DataCacheBLL()
{
icache = (ICache)DALFactory.DataFactory.GetFactoryDataCache();
}
public static DataCacheBLL Getinstance()
{
if (instance == null)
{
lock (lockHelp)
{
if (instance == null)
{
instance = new DataCacheBLL();
}
}
}
return instance;
}
/// <summary>
/// 获取当前应用程序指定CacheKey的Cache值
/// </summary>
/// <param name="CacheKey"></param>
/// <returns></returns>
public object GetCache(string CacheKey)
{
return icache.GetCache(CacheKey);
}
/// <summary>
/// 设置当前应用程序指定CacheKey的Cache值
/// </summary>
/// <param name="CacheKey"></param>
/// <param name="objObject"></param>
public void SetCache(string CacheKey, object objObject)
{
icache.SetCache(CacheKey, objObject);
}
/// <summary>
/// 设置当前应用程序指定CacheKey的Cache值
/// </summary>
/// <param name="CacheKey"></param>
/// <param name="objObject"></param>
public void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
{
icache.SetCache(CacheKey, objObject, absoluteExpiration, slidingExpiration);
}
/// <summary>
/// 绝对过期时间
/// </summary>
/// <param name="CacheKey"></param>
/// <param name="objObject"></param>
public void SetCacheAbsolute(string CacheKey, object objObject)
{
icache.SetCache(CacheKey, objObject, DateTime.Now.AddMinutes(intcachetime), TimeSpan.Zero);
}
/// <summary>
/// 相对过期时间
/// </summary>
/// <param name="CacheKey"></param>
/// <param name="objObject"></param>
public void SetCacheSliding(string CacheKey, object objObject)
{
icache.SetCache(CacheKey, objObject, DateTime.MaxValue, TimeSpan.FromMinutes(intcachetime));
}
/// <summary>
/// 还可以设置文件依赖和数据库依赖
/// </summary>
/// <param name="CacheKey"></param>
/// <param name="objObject"></param>
/// <param name="dep"></param>
public void SetCache(string CacheKey, object objObject, CacheDependency dep)
{
icache.SetCache(CacheKey, objObject, dep);
}
/// <summary>
/// 清除单一键缓存
/// </summary>
/// <param name="key"></param>
public void RemoveOneCache(string CacheKey)
{
icache.RemoveOneCache(CacheKey);
}
/// <summary>
/// 清除所有缓存
/// </summary>
public void RemoveAllCache()
{
icache.RemoveAllCache();
}
/// <summary>
/// 以列表形式返回已存在的所有缓存
/// </summary>
/// <returns></returns>
public List<string> ShowAllCache()
{
return icache.ShowAllCache();
}
调用
DataCacheBLL dcbll = DataCacheBLL.Getinstance();
public DataSet GetSectionList()
{
string CacheKey = "key1";
DataSet ds = (DataSet)dcbll.GetCache(CacheKey);
if (ds == null)
{
try
{
ds = ipro.GetList();
if (ds != null)
{
dcbll.SetCacheAbsolute(CacheKey, ds);
}
}
catch
{ }
}
return ds;
}
注:特别注意在调用缓存时做日志记录时如果你用的是StreamWriter.AutoFlush = true;设置记录日志,
此时将会对Cache影响丢失,出现每次请求时重新做缓存现象.
最后
以上就是重要墨镜为你收集整理的asp.net 数据缓存System.Web.Caching.Cache 实例运用的全部内容,希望文章能够帮你解决asp.net 数据缓存System.Web.Caching.Cache 实例运用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复