C#自定义缓存封装类实例
本文实例讲述了C#自定义缓存封装类。分享给大家供大家参考。具体如下:
这个自定义的C#类封装了部分常用的缓存操作,包括写入缓存,读取缓存,设置缓存过期时间等等,简化了C#的缓存操作,代码非常简单,易于阅读。
using System; using System.Web; namespace DotNet.Utilities { /// <summary> /// 缓存相关的操作类 /// </summary> public class DataCache { /// <summary> /// 获取当前应用程序指定CacheKey的Cache值 /// </summary> /// <param name="CacheKey"></param> /// <returns></returns> public static object GetCache(string CacheKey) { System.Web.Caching.Cache objCache = HttpRuntime.Cache; return objCache[CacheKey]; } /// <summary> /// 设置当前应用程序指定CacheKey的Cache值 /// </summary> /// <param name="CacheKey"></param> /// <param name="objObject"></param> public static void SetCache(string CacheKey, object objObject) { System.Web.Caching.Cache objCache = HttpRuntime.Cache; objCache.Insert(CacheKey, objObject); } /// <summary> /// 设置当前应用程序指定CacheKey的Cache值 /// </summary> /// <param name="CacheKey"></param> /// <param name="objObject"></param> public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration,TimeSpan slidingExpiration ) { System.Web.Caching.Cache objCache = HttpRuntime.Cache; objCache.Insert(CacheKey, objObject,null,absoluteExpiration,slidingExpiration); } } }
希望本文所述对大家的C#程序设计有所帮助。
赞 (0)