应用程序数据缓存

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinqDemoTest
{
    /// <summary>  
    /// .net 本身自带的cache相关方法  
    /// </summary>  
    public static class CacheHelp
    {
        /// <summary>  
        /// fileds  
        /// </summary>  
        private static System.Web.Caching.Cache ObjCache = System.Web.HttpRuntime.Cache;

        #region Exist方法  
        /// <summary>  
        /// 判断指定Key的缓存是否存在  
        /// </summary>  
        /// <param name="Key"></param>  
        /// <returns></returns>  
        public static bool Exist(string Key)
        {
            if (ObjCache[Key] == null)
            {
                return false;
            }
            return true;
        }
        #endregion

        #region  Get方法  
        /// <summary>  
        /// 获得指定Key的缓存对象  
        /// </summary>  
        /// <param name="Key"></param>  
        /// <returns></returns>  
        public static Object Get(string Key)
        {
            object objkey = null;
            if (ObjCache[Key] != null)
            {
                objkey = ObjCache.Get(Key);
            }
            return objkey;
        }
        #endregion

        #region Set方法  

        /// <summary>  
        /// 设置缓存  
        /// </summary>  
        /// <param name="Key">Cache Key</param>  
        /// <param name="expiry">缓存时间</param>  
        /// <param name="obj">缓存对象</param>  
        public static void Set(string Key, DateTime expiry, object obj)
        {
            if (ObjCache[Key] != null)
            {
                ObjCache.Remove(Key);
            }

            ObjCache.Insert(Key, obj, null, expiry, TimeSpan.Zero);
        }

        /// <summary>  
        /// 设置缓存  
        /// </summary>  
        /// <param name="Key">Cache Key</param>  
        /// <param name="min">缓存时间【分钟】</param>  
        /// <param name="obj">缓存对象</param>  
        public static void Set(string Key, int min, object obj)
        {
            double douNum = double.Parse(min.ToString());
            Set(Key, DateTime.Now.AddMinutes(douNum), obj);
        }
        #endregion

        #region Del方法  

        /// <summary>  
        /// 删除指定Key的缓存  
        /// </summary>  
        /// <param name="Key"></param>  
        public static void Del(string Key)
        {
            if (ObjCache[Key] != null)
            {
                ObjCache.Remove(Key);
            }
        }
        #endregion

        #region 其他  

        /// <summary>  
        /// 获取缓存中的项数  
        /// </summary>  
        public static int Count
        {
            get
            {
                return ObjCache.Count;
            }
        }

        /// <summary>  
        /// 获取可用于缓存的千字节数  
        /// </summary>  
        public static long PrivateBytes
        {
            get
            {
                return ObjCache.EffectivePrivateBytesLimit;
            }
        }
        #endregion
    }
}



ps:缓存时间 为时间类型 有以下两种处理方式

1.设定绝对过期时间
/// <summary>
        /// 设定绝对的过期时间
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <param name="objObject"></param>
        /// <param name="seconds">超过多少秒后过期</param>
        public static void SetCacheDateTime(string CacheKey, object objObject, long Seconds) 
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(CacheKey, objObject, null, System.DateTime.Now.AddSeconds(Seconds), TimeSpan.Zero);
        }

 

2.设定相对过期时间
/// <summary>
        /// 设置当前应用程序指定包含相对过期时间Cache值
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <param name="objObject"></param>
        /// <param name="timeSpan">超过多少时间不调用就失效,单位是秒</param>
        public static void SetCacheTimeSpan(string CacheKey, object objObject,long timeSpan) 
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(CacheKey, objObject, null, DateTime.MaxValue, TimeSpan.FromSeconds(timeSpan));
        }

参考资料:

http://www.cnblogs.com/wuyifu/p/3956476.html
http://blog.csdn.net/lybwwp/article/details/18253341
posted @ 2016-08-19 10:35  春秋一梦  阅读(328)  评论(0编辑  收藏  举报