123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- using CP.Cache.Redis;
- using CP.Cache.WebCache;
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Diagnostics;
- using System.Reflection;
- using System.Web;
- namespace CP.Cache
- {
- public class WMCache
- {
- /// <summary>
- /// 本类实体
- /// </summary>
- private static volatile WMCache instance = null;
- /// <summary>
- /// cache 接口
- /// </summary>
- private static ICache mcs;
- /// <summary>
- /// redis接口
- /// </summary>
- private static ICache rcs;
- /// <summary>
- /// 锁
- /// </summary>
- private static object lockHelper = new object();
- /// <summary>
- /// 初始化Cache
- /// </summary>
- private WMCache()
- {
- switch (CacheProvider)
- {
- case 0:
- mcs = new WebCacheManager(); //web memory cache
- break;
- case 1:
- rcs = new RedisManager(); //redis cache
- mcs = new WebCacheManager(); //web memory cache
- break;
- default:
- mcs = new WebCacheManager(); //web memory cache
- break;
- }
- }
- /// <summary>
- /// 获取缓存实例
- /// </summary>
- /// <returns></returns>
- public static WMCache GetCacheService()
- {
- if (instance == null)
- {
- lock (lockHelper)
- {
- if (instance == null)
- {
- instance = new WMCache();
- }
- }
- }
- return instance;
- }
- /// <summary>
- /// 添加T到缓存
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="key"></param>
- /// <param name="o"></param>
- /// <param name="t"></param>
- /// <param name="IsRedis">是否使用redis</param>
- public virtual void AddObject<T>(string key, T o, int t = 60, bool IsRedis = false)
- {
- if (string.IsNullOrEmpty(key) || o == null)
- return;
- lock (lockHelper)
- {
- //HttpContext.Current.Response.Write("SetObject:" + key + "<br/>");
- if (IsRedis)
- {
- rcs.AddObject<T>(key, o, t);
- }
- else
- {
- mcs.AddObject<T>(key, o, t);
- }
- }
- }
- /// <summary>
- /// 根据Key获取key值
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="key"></param>
- /// <returns></returns>
- public virtual T GetObject<T>(string key, bool IsRedis = false)
- {
- //StackTrace ss = new StackTrace(true);
- //MethodBase mb = ss.GetFrame(2).GetMethod();
- //HttpContext.Current.Response.Write("名称空间:" + mb.DeclaringType.Namespace + ",类名:" + mb.DeclaringType.Name + ",方法名:" + mb.Name + "<br/>");
- //HttpContext.Current.Response.Write("Get:" + key + "<br/><br />");
- if (string.IsNullOrEmpty(key))
- return default(T);
- lock (lockHelper)
- {
- if (IsRedis)
- {
- return rcs.GetObject<T>(key);
- }
- else
- {
- return mcs.GetObject<T>(key);
- }
- }
- }
- /// <summary>
- /// key是否存在...
- /// </summary>
- /// <param name="key"></param>
- /// <param name="IsRedis">是否使用redis</param>
- /// <returns></returns>
- public virtual bool IsExist(string key, bool IsRedis = false)
- {
- lock (lockHelper)
- {
- if (IsRedis)
- {
- return rcs.IsExist(key);
- }
- else {
- return mcs.IsExist(key);
- }
- }
- }
- public virtual List<string> GetKeys(bool IsRedis = false)
- {
- lock (lockHelper)
- {
- if (IsRedis)
- {
- return rcs.GetKeys();
- }
- else
- {
- return mcs.GetKeys();
- }
- }
- }
- public virtual void RemoveObject(string key)
- {
- if (string.IsNullOrEmpty(key))
- return;
- lock (lockHelper)
- {
- mcs.RemoveObject(key);
- rcs?.RemoveObject(key);
- }
- }
- /// <summary>
- /// 移出所有cache
- /// </summary>
- public virtual void RemoveAll()
- {
- lock (lockHelper)
- {
- mcs.RemoveAll();
- rcs?.RemoveAll();
- }
- }
- /// <summary>
- /// 批量移出cache
- /// </summary>
- /// <param name="key"></param>
- public virtual void RemoveObjectByRegex(string key)
- {
- if (string.IsNullOrEmpty(key))
- return;
- lock (lockHelper)
- {
- mcs.RemoveObjectByRegex(key);
- rcs?.RemoveObjectByRegex(key);
- }
- }
- /// <summary>
- /// 是否开启IsMemCached模型
- /// 请查看web.config的参数配置
- /// </summary>
- private static int CacheProvider
- {
- get
- {
- if (ConfigurationManager.AppSettings["CacheProvider"] != null)
- return Convert.ToInt32(ConfigurationManager.AppSettings["CacheProvider"]);
- return 0;
- }
- }
- }
- }
|