????????????????????Ч???????????????????
????1  public enum CacheType
????2     {
????3         RedisCache??
????4
????5         MemcachedCache
????6     }
????7
????8     public class CacheRepository
????9     {
????10
????11         public static BaseCache Current(CacheType cacheType = CacheType.RedisCache)
????12         {
????13             var nspace = typeof(BaseCache);
????14             var fullName = nspace.FullName;
????15             var nowspace = fullName.Substring(0?? fullName.LastIndexOf('.') + 1);
????16        
????17             return Assembly.GetExecutingAssembly().CreateInstance(nowspace + cacheType.ToString()?? true) as BaseCache;
????18         }
????19     }
????20
????21     public class BaseCache : IDisposable
????22     {
????23         protected string def_ip = string.Empty;
????24         protected int def_port = 0;
????25         protected string def_password = string.Empty;
????26
????27         public BaseCache()
????28         {
????29
????30         }
????31
????32         public virtual void InitCache(string ip = ""?? int port = 0?? string password = "")
????33         {
????34
????35         }
????36
????37         public virtual bool SetCache<T>(string key?? T t?? int timeOutMinute = 10) where T : class??new()
????38         {
????39
????40             return false;
????41         }
????42
????43         public virtual T GetCache<T>(string key) where T : class??new()
????44         {
????45
????46             return default(T);
????47         }
????48
????49         public virtual bool Remove(string key)
????50         {
????51
????52             return false;
????53         }
????54
????55         public virtual bool FlushAll()
????56         {
????57
????58             return false;
????59         }
????60
????61         public virtual bool Any(string key)
????62         {
????63
????64             return false;
????65         }
????66
????67         public virtual void Dispose(bool isfalse)
????68         {
????69
????70             if (isfalse)
????71             {
????72
????73
????74             }
????75         }
????76
????77         //??????
????78         public void Dispose()
????79         {
????80
????81             this.Dispose(true);
????82             //????????
????83             GC.SuppressFinalize(this);
????84         }
????85     }
????86
????87     /// <summary>
????88     /// Redis????
????89     /// </summary>
????90     public class RedisCache : BaseCache
????91     {
????92         public RedisClient redis = null;
????93
????94         public RedisCache()
????95         {
????96
????97             //??????????????????????
????98             def_ip = "172.16.2.56";
????99             def_port = 6379;
????100             def_password = "";
????101         }
????102
????103         #region Redis????
????104
????105         public override void InitCache(string ip = ""?? int port = 0?? string password = "")
????106         {
????107
????108             if (redis == null)
????109             {
????110                 ip = string.IsNullOrEmpty(ip) ? def_ip : ip;
????111                 port = port == 0 ? def_port : port;
????112                 password = string.IsNullOrEmpty(password) ? def_password : password;
????113
????114                 redis = new RedisClient(ip?? port?? password);
????115             }
????116         }
????117
????118         public override bool SetCache<T>(string key?? T t?? int timeOutMinute = 10)
????119         {
????120
????121             var isfalse = false;
????122
????123             try
????124             {
????125                 if (string.IsNullOrEmpty(key)) { return isfalse; }
????126
????127                 InitCache();
????128                 isfalse = redis.Set<T>(key?? t?? TimeSpan.FromMinutes(timeOutMinute));
????129             }
????130             catch (Exception ex)
????131             {
????132             }
????133             finally { this.Dispose(); }
????134             return isfalse;
????135         }
????136
????137         public override T GetCache<T>(string key)
????138         {
????139             var t = default(T);
????140             try
????141             {
????142                 if (string.IsNullOrEmpty(key)) { return t; }
????143
????144                 InitCache();
????145                 t = redis.Get<T>(key);
????146             }
????147             catch (Exception ex)
????148             {
????149             }
????150             finally { this.Dispose(); }
????151             return t;
????152         }
????153
????154         public override bool Remove(string key)
????155         {
????156             var isfalse = false;
????157             try
????158             {
????159                 if (string.IsNullOrEmpty(key)) { return isfalse; }
????160
????161                 InitCache();
????162                 isfalse = redis.Remove(key);
????163             }
????164             catch (Exception ex)
????165             {
????166             }
????167             finally { this.Dispose(); }
????168             return isfalse;
????169         }
????170
????171         public override void Dispose(bool isfalse)
????172         {
????173
????174             if (isfalse && redis != null)
????175             {
????176
????177                 redis.Dispose();
????178                 redis = null;
????179             }
????180         }
????181
????182         #endregion
????183     }
????184
????185
????186     /// <summary>
????187     /// Memcached????
????188     /// </summary>
????189     public class MemcachedCache : BaseCache
????190     {
????191
????192
????193     }