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
- {
-
-
-
- private static volatile WMCache instance = null;
-
-
-
- private static ICache mcs;
-
-
-
- private static ICache rcs;
-
-
-
- private static object lockHelper = new object();
-
-
-
- private WMCache()
- {
- switch (CacheProvider)
- {
- case 0:
- mcs = new WebCacheManager();
- break;
- case 1:
- rcs = new RedisManager();
- mcs = new WebCacheManager();
- break;
- default:
- mcs = new WebCacheManager();
- break;
- }
- }
-
-
-
-
- public static WMCache GetCacheService()
- {
- if (instance == null)
- {
- lock (lockHelper)
- {
- if (instance == null)
- {
- instance = new WMCache();
- }
- }
- }
- return instance;
- }
-
-
-
-
-
-
-
-
- public virtual void AddObject<T>(string key, T o, int t = 60, bool IsRedis = false)
- {
- if (string.IsNullOrEmpty(key) || o == null)
- return;
- lock (lockHelper)
- {
-
- if (IsRedis)
- {
- rcs.AddObject<T>(key, o, t);
- }
- else
- {
- mcs.AddObject<T>(key, o, t);
- }
- }
- }
-
-
-
-
-
-
- public virtual T GetObject<T>(string key, bool IsRedis = false)
- {
-
-
-
-
- if (string.IsNullOrEmpty(key))
- return default(T);
- lock (lockHelper)
- {
- if (IsRedis)
- {
- return rcs.GetObject<T>(key);
- }
- else
- {
- return mcs.GetObject<T>(key);
- }
- }
- }
-
-
-
-
-
-
- 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);
- }
- }
-
-
-
- public virtual void RemoveAll()
- {
- lock (lockHelper)
- {
- mcs.RemoveAll();
- rcs?.RemoveAll();
- }
- }
-
-
-
-
- public virtual void RemoveObjectByRegex(string key)
- {
- if (string.IsNullOrEmpty(key))
- return;
- lock (lockHelper)
- {
- mcs.RemoveObjectByRegex(key);
- rcs?.RemoveObjectByRegex(key);
- }
- }
-
-
-
-
- private static int CacheProvider
- {
- get
- {
- if (ConfigurationManager.AppSettings["CacheProvider"] != null)
- return Convert.ToInt32(ConfigurationManager.AppSettings["CacheProvider"]);
- return 0;
- }
- }
- }
- }
|