现在的位置: 首页 > 综合 > 正文

缓存技术,封装好的缓存类

2017年11月06日 ⁄ 综合 ⁄ 共 2195字 ⁄ 字号 评论关闭
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Admin.Helper
{
    /// <summary>
    /// .net 本身自带的cache相关方法
    /// </summary>
    public static class CacheHelper
    {
        /// <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
    }
}
<pre code_snippet_id="155522" snippet_file_name="blog_20140114_2_3856905" name="code" class="csharp">  使用方法如下:

/// <summary>
        /// 获取产品详情
        /// </summary>
        /// <param name="proId"></param>
        /// <returns></returns>
        public static ProductDto GetProduct(string proId)
        {
            var key = "_ProductId" + proId;
            //通过key找到缓存的对象
            var cache = Helper.CacheHelper.Get(key);
            if (cache != null)
            {
                return (ProductDto)cache;
            }
            var pro = ServiceLocator.Create<IProductService>().Get(proId);
            if (pro == null) return new ProductDto();
            Helper.CacheHelper.Set(key, DateTime.Now.AddHours(8), pro);
            return pro;
        }




抱歉!评论已关闭.