IWeb_Cache

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Caching;   public class IWeb_Cache     {         protected static volatile System.Web.Caching.Cache webCache = System.Web.HttpRuntime.Cache;

        /// <summary>         /// 默认缓存存活期为3600秒(1小时)         /// </summary>         protected int _timeOut = 3600;

        private static object syncObj = new object();

        /// <summary>         /// 设置到期相对时间[单位: 秒]         /// </summary>         public virtual int TimeOut         {             set { _timeOut = value > 0 ? value : 3600; }             get { return _timeOut > 0 ? _timeOut : 3600; }         }

        public static System.Web.Caching.Cache GetWebCacheObj         {             get { return webCache; }         }

        /// <summary>         /// 加入当前对象到缓存中         /// </summary>         /// <param name="CACHEKEY">对象的键值</param>         /// <param name="o">缓存的对象</param>         public virtual void AddObjectToCache(string CACHEKEY, object CACHE_VALUE)         {             if (CACHEKEY == null || CACHEKEY.Length == 0 || CACHE_VALUE == null)             {                 return;             }

            if (TimeOut == 7200)             {                 webCache.Insert(CACHEKEY, CACHE_VALUE, null, DateTime.MaxValue, TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, null);             }             else             {                 webCache.Insert(CACHEKEY, CACHE_VALUE, null, DateTime.Now.AddSeconds(TimeOut), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, null);             }         }

        /// <summary>         /// 加入当前对象到缓存中         /// </summary>         /// <param name="CACHEKEY">对象的键值</param>         /// <param name="CACHE_VALUE">缓存的对象</param>         /// <param name="o">到期时间,单位:秒</param>         public virtual void AddObjectToCache(string CACHEKEY, object CACHE_VALUE, int expire)         {             if (CACHEKEY == null || CACHEKEY.Length == 0 || CACHE_VALUE == null)             {                 return;             }

            //表示永不过期             if (expire == 0)             {                 webCache.Insert(CACHEKEY, CACHE_VALUE, null, DateTime.MaxValue, TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, null);             }             else             {                 webCache.Insert(CACHEKEY, CACHE_VALUE, null, DateTime.Now.AddSeconds(expire), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, null);             }         }

        /// <summary>         /// 加入当前对象到缓存中,并对相关文件建立依赖         /// </summary>         /// <param name="CACHEKEY">对象的键值</param>         /// <param name="CACHE_VALUE">缓存的对象</param>         /// <param name="files">监视的路径文件</param>         public virtual void AddObjectWithFileChange(string CACHEKEY, object CACHE_VALUE, string[] files)         {             if (CACHEKEY == null || CACHEKEY.Length == 0 || CACHE_VALUE == null)             {                 return;             }

            CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);

            CacheDependency dep = new CacheDependency(files, DateTime.Now);

            webCache.Insert(CACHEKEY, CACHE_VALUE, dep, System.DateTime.Now.AddSeconds(TimeOut), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, callBack);         }

 

        /// <summary>         /// 加入当前对象到缓存中,并使用依赖键         /// </summary>         /// <param name="CACHEKEY">对象的键值</param>         /// <param name="o">缓存的对象</param>         /// <param name="dependKey">依赖关联的键值</param>         public virtual void AddObjectWithDepend(string CACHEKEY, object CACHE_VALUE, string[] dependKey)         {             if (CACHEKEY == null || CACHEKEY.Length == 0 || CACHE_VALUE == null)             {                 return;             }

            CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);

            CacheDependency dep = new CacheDependency(null, dependKey, DateTime.Now);

            webCache.Insert(CACHEKEY, CACHE_VALUE, dep, System.DateTime.Now.AddSeconds(TimeOut), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, callBack);         }

        /// <summary>         /// 建立回调委托的一个实例         /// </summary>         /// <param name="key"></param>         /// <param name="val"></param>         /// <param name="reason"></param>         public void onRemove(string key, object val, CacheItemRemovedReason reason)         {             switch (reason)             {                 case CacheItemRemovedReason.DependencyChanged:                     break;                 case CacheItemRemovedReason.Expired:                     {                         break;                     }                 case CacheItemRemovedReason.Removed:                     {                         break;                     }                 case CacheItemRemovedReason.Underused:                     {                         break;                     }                 default: break;             }

        }

        /// <summary>         /// 删除缓存对象         /// </summary>         /// <param name="CACHEKEY">对象的关键字</param>         public virtual void RemoveObject(string CACHEKEY)         {             if (CACHEKEY == null || CACHEKEY.Length == 0)             {                 return;             }             webCache.Remove(CACHEKEY);         }

        /// <summary>         /// 返回一个指定的对象         /// </summary>         /// <param name="CACHEKEY">对象的关键字</param>         /// <returns>对象</returns>         public virtual object GetCacheObjectByCachekey(string CACHEKEY)         {             if (CACHEKEY == null || CACHEKEY.Length == 0)             {                 return null;             }             return webCache.Get(CACHEKEY);         }     } 

posted @ 2013-01-24 14:34  greefsong  阅读(115)  评论(0编辑  收藏  举报