.net Cache的用法

时间:2022-10-25 17:00:23

HttpContext.Current.Cache 使用方法

.net Cache 怎么使用

/// <summary>
        /// 简单创建/修改Cache,前提是这个值是字符串形式的
        /// </summary>
        /// <param name="strCacheName">Cache名称</param>
        /// <param name="strValue">Cache值</param>
        /// <param name="iExpires"></param>
        /// <param name="inType">1为 在iExpires分过期 绝对时间 ,2为在iExpires之后如果没有人访问 则移除</param>
        public static void Insert(string strCacheName, object strValue, int iExpires, int inType)
        {
            //缓存依赖项
            CacheDependency mydepen = new CacheDependency(HttpContext.Current.Server.MapPath("~") + @"//cache.xml");
            if (inType == 1)
            {
                DateTime absoluteExpiration = DateTime.Now.AddMinutes(iExpires);
                HttpContext.Current.Cache.Insert(strCacheName, strValue, mydepen, absoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
            }
            else if (inType == 2)
            {
                TimeSpan slidingExpiration = TimeSpan.FromMinutes(iExpires);
                HttpContext.Current.Cache.Insert(strCacheName, strValue, mydepen, System.Web.Caching.Cache.NoAbsoluteExpiration, slidingExpiration, CacheItemPriority.Normal, null);

}
        }