封装缓存(static构造函数介绍)

1、静态类只能有一个静态构造函数,静态构造函数不能被重载;

2、调用静态类中的方法时,静态构造函数只调用一次;

3、静态类不能实例化(不能new);

4、静态字段线程中是共享;

5、静态构造函数必须无参数;

6、静态构造函数不允许访问修饰符并且不能接受任何参数;

7、静态类中只能有静态成员;

CacheRepository

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

namespace CommonLibrary.Caching
{
    public static class CacheRepository
    {
        private static ICache cache = null;
        private const String CacheProvider = "CommonLibrary.Caching.WebCache,CommonLibrary";
        /// <summary>
        /// 静态构造函数,只调用一次
        /// </summary>
        static CacheRepository()
        {
            double cacheExpirationFactor = 1.0d;
            Type cacheProviderType = Type.GetType(CacheProvider);
            //实例化WebCache类,由于构造函数只调用一次,所以WebCahce只实例化一次?
            cache = Activator.CreateInstance(cacheProviderType, new object[] { cacheExpirationFactor }) as ICache;
        }
        /// <summary>
        /// 添加到缓存
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <param name="value"></param>
        /// <param name="cachingExpirationType"></param>
        public static void Add(string cacheKey,object value,CachingExpirationTypes cachingExpirationType)
        {
            cache.Add(cacheKey, value, cachingExpirationType);
        }
        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <returns></returns>
        public static object Get(string cacheKey)
        {
            return cache.Get(cacheKey);
        }
        /// <summary>
        /// 清除缓存
        /// </summary>
        /// <param name="cacheKey"></param>
        public static void Remove(string cacheKey)
        {
            cache.Remove(cacheKey);
        }
        /// <summary>
        /// 清空缓存
        /// </summary>
        public static void Clear()
        {
            cache.Clear();
        }
    }
}

ICahce

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

namespace CommonLibrary.Caching
{
    /// <summary>
    /// 缓存服务接口
    /// </summary>
    public interface ICache
    {
        /// <summary>
        /// 加入缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="type">缓存期限类型</param>
        void Add(string key, object value, CachingExpirationTypes type);
        /// <summary>
        /// 加入缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="timeSpan">缓存失效时间</param>
        void Add(string key, object value, TimeSpan timeSpan);
        void AddOrReplace(string key, object value, CachingExpirationTypes type);
        void AddOrReplace(string key, object value, TimeSpan timeSpan);
        object Get(string cacheKey);
        void Remove(string cacheKey);
        void Clear();
        void Clear(string cahceKey);
    }
}

WebCache

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching;

namespace CommonLibrary.Caching
{
    /// <summary>
    /// System.Web.Caching.Cache实现ICahce
    /// </summary>
    public class WebCache:ICache
    {
        private readonly Cache _cache;
        private readonly Dictionary<CachingExpirationTypes, TimeSpan> CachingExpirationDic;
        public WebCache()
        {
            HttpContext context = HttpContext.Current;
            if (null == context)
            {
                _cache = HttpRuntime.Cache;
            }
            else
            {
                _cache = context.Cache;
            }
        }
        public WebCache(double cacheExpirationFactor)
        {
            HttpContext context = HttpContext.Current;
            if (null == context)
            {
                _cache = HttpRuntime.Cache;
            }
            else
            {
                _cache = context.Cache;
            }
            CachingExpirationDic = new Dictionary<CachingExpirationTypes, TimeSpan>();
            CachingExpirationDic.Add(CachingExpirationTypes.Invariable, new TimeSpan(0, 0, (int)(24 * 60 * 60 * cacheExpirationFactor)));
            CachingExpirationDic.Add(CachingExpirationTypes.Stable, new TimeSpan(0, 0, (int)(8 * 60 * 60 * cacheExpirationFactor)));
            CachingExpirationDic.Add(CachingExpirationTypes.RelativelyStable, new TimeSpan(0, 0, (int)(2 * 60 * 60 * cacheExpirationFactor)));
            CachingExpirationDic.Add(CachingExpirationTypes.UsualSingleObject, new TimeSpan(0, 0, (int)(10 * 60 * cacheExpirationFactor)));
            CachingExpirationDic.Add(CachingExpirationTypes.UsualObjectCollection, new TimeSpan(0, 0, (int)(5 * 60 * cacheExpirationFactor)));
        }

        public void Add(string key, object value, CachingExpirationTypes type)
        {
            if (null == key || value == null) return;
            Add(key, value, CachingExpirationDic[type]);
        }

        public void Add(string key, object value, TimeSpan timeSpan)
        {
            if (string.IsNullOrWhiteSpace(key) || null == value) return;
            _cache.Add(key, value, null, DateTime.Now.Add(timeSpan), TimeSpan.Zero, CacheItemPriority.Normal, null);
        }

        public void AddOrReplace(string key, object value, CachingExpirationTypes type)
        {
            Remove(key);
            Add(key, value, CachingExpirationDic[type]);
        }

        public void AddOrReplace(string key, object value, TimeSpan timeSpan)
        {
            Remove(key);
            Add(key, value, timeSpan);
        }

        public object Get(string cacheKey)
        {
            return _cache[cacheKey];
        }

        public void Remove(string cacheKey)
        {
            IDictionaryEnumerator cacheEnum = _cache.GetEnumerator();
            while (cacheEnum.MoveNext())
            {
                if (cacheEnum.Key.ToString().StartsWith(cacheKey))
                {
                    _cache.Remove(cacheKey);
                }
            }
        }
        /// <summary>
        /// 清除所有缓存项
        /// </summary>
        public void Clear()
        {
            IDictionaryEnumerator cacheEnum = _cache.GetEnumerator();
            while (cacheEnum.MoveNext())
            {
                _cache.Remove(cacheEnum.Key.ToString());
            }
        }
        /// <summary>
        /// 清除缓存项
        /// </summary>
        /// <param name="cahceKey"></param>
        public void Clear(string cahceKey)
        {
            IDictionaryEnumerator cacheEnum = _cache.GetEnumerator();
            while (cacheEnum.MoveNext())
            {
                if (cacheEnum.Key.ToString() == cahceKey)
                {
                    _cache.Remove(cacheEnum.Key.ToString());
                    break;
                }
            }
        }
    }
}

CachingExpirationTypes

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

namespace CommonLibrary.Caching
{
    /// <summary>
    /// 缓存期限类型
    /// </summary>
    public enum CachingExpirationTypes
    {
        /// <summary>
        /// 永久不变(24小时)
        /// </summary>
        Invariable=0,
        /// <summary>
        /// 稳定数据8小时
        /// </summary>
        Stable=1,
        /// <summary>
        /// 相对稳定(2个小时)权限
        /// </summary>
        RelativelyStable=2,
        /// <summary>
        /// 常用单个对象(10分钟)
        /// </summary>
        UsualSingleObject=3,
        /// <summary>
        /// 例如用户信息(5分钟)
        /// </summary>
        UsualObjectCollection=4
    }
}

CacheKeys

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

namespace CommonLibrary.Caching
{
    public class CacheKeys
    {
    }
}

 

posted @ 2016-08-12 15:59  wjl910  阅读(221)  评论(0)    收藏  举报