Exemplo n.º 1
0
        public ICacheResult <T> Set <T>(string key, T value, int expiredTime = 3600)
        {
            CacheResult <T> cacheResult = new CacheResult <T>()
            {
                Success = false,
                Status  = Status.None
            };
            CacheResult <T> cacheResult1 = cacheResult;

            expiredTime = (expiredTime <= 0 ? 3600 : expiredTime);
            try
            {
                SafeBucket instance = SafeBucket.Instance;
                try
                {
                    IOperationResult <T> operationResult = instance.Bucket.Upsert <T>(KeyGen.AttachPrefixToKey(key), value, new TimeSpan(0, 0, 0, expiredTime));
                    DebugUtil.CollectDebugInfo(string.Format("Set<T>('{0}'), Status:{1}, Exception:{2}", key, operationResult.Status, (operationResult.Exception == null ? "null" : operationResult.Exception.Message)));
                    cacheResult1 = new CacheResult <T>(operationResult);
                }
                finally
                {
                    if (instance != null)
                    {
                        ((IDisposable)instance).Dispose();
                    }
                }
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                cacheResult1.Exception = exception;
                DebugUtil.CollectDebugInfo(string.Format("Set<T>('{0}'), Exception:{1}", key, exception.Message));
            }
            return(cacheResult1);
        }
Exemplo n.º 2
0
        public ICacheResult Remove(string key)
        {
            CacheResult cacheResult = new CacheResult()
            {
                Success = false,
                Status  = Status.None
            };
            CacheResult cacheResult1 = cacheResult;

            try
            {
                SafeBucket instance = SafeBucket.Instance;
                try
                {
                    IOperationResult operationResult = instance.Bucket.Remove(KeyGen.AttachPrefixToKey(key));
                    DebugUtil.CollectDebugInfo(string.Format("Remove('{0}'), Status:{1}, Exception:{2}", key, operationResult.Status, (operationResult.Exception == null ? "null" : operationResult.Exception.Message)));
                    cacheResult1 = new CacheResult(operationResult);
                }
                finally
                {
                    if (instance != null)
                    {
                        ((IDisposable)instance).Dispose();
                    }
                }
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                cacheResult1.Exception = exception;
                DebugUtil.CollectDebugInfo(string.Format("Remove('{0}'), Exception:{1}", key, exception.Message));
            }
            return(cacheResult1);
        }
Exemplo n.º 3
0
        public ICacheResult Remove(string key)
        {
            ICacheResult cacheResult;

            lock (Cache)
            {
                object obj = Cache.Remove(KeyGen.AttachPrefixToKey(key), null);
                cacheResult = new CacheResult(obj);
            }
            return(cacheResult);
        }
Exemplo n.º 4
0
        public ICacheResult Remove(string key)
        {
            ICacheResult cacheResult = new CacheResult()
            {
                Success = false,
                Status  = Status.None
            };
            ICacheResult cacheResult1 = cacheResult;

            try
            {
                cacheResult1.Success = this.db.KeyDelete(KeyGen.AttachPrefixToKey(key), 0);
                cacheResult1.Status  = (cacheResult1.Success ? Status.Success : Status.None);
            }
            catch (Exception exception)
            {
                cacheResult1.Exception = exception;
            }
            return(cacheResult1);
        }
Exemplo n.º 5
0
        public ICacheResult <T> Get <T>(string key)
        {
            CacheResult <T> cacheResult = new CacheResult <T>()
            {
                Success = false,
                Status  = Status.None
            };
            CacheResult <T> cacheResult1 = cacheResult;

            try
            {
                RedisValue redisValue = this.db.StringGet(KeyGen.AttachPrefixToKey(key), 0);
                cacheResult1.Value   = JsonSerializerUtil.Deserialize <T>(redisValue);
                cacheResult1.Success = true;
                cacheResult1.Status  = Status.Success;
            }
            catch (Exception exception)
            {
                cacheResult1.Exception = exception;
            }
            return(cacheResult1);
        }
Exemplo n.º 6
0
        public ICacheResult <T> Set <T>(string key, T value, int expiredTime = 3600)
        {
            CacheResult <T> cacheResult = new CacheResult <T>()
            {
                Success = false,
                Status  = Status.None
            };
            CacheResult <T> cacheResult1 = cacheResult;

            expiredTime = (expiredTime <= 0 ? 3600 : expiredTime);
            try
            {
                string str = JsonSerializerUtil.Serialize <T>(value);
                cacheResult1.Success = this.db.StringSet(KeyGen.AttachPrefixToKey(key), str, new TimeSpan?(new TimeSpan(0, 0, expiredTime)), 0, 0);
                cacheResult1.Status  = Status.Success;
            }
            catch (Exception exception)
            {
                cacheResult1.Exception = exception;
            }
            return(cacheResult1);
        }
Exemplo n.º 7
0
        public ICacheResult ResetKeys(string keyPrefix = "")
        {
            CacheResult cacheResult = new CacheResult()
            {
                Success = false,
                Status  = Status.None
            };
            CacheResult cacheResult1 = cacheResult;

            try
            {
                SafeBucket instance = SafeBucket.Instance;
                try
                {
                    IBucket  bucket = instance.Bucket;
                    object[] appKey = new object[] { AppContext.AppKey, keyPrefix, SafeBucket.Instance.Bucket.Name, KeyGen.AttachPrefixToKey(keyPrefix) };
                    bucket.Query <object>(string.Format("CREATE INDEX {0}_{1}_KeyPrefix_Index ON {2}(position((meta().id), \"{3}\")) USING GSI", appKey));
                    IQueryResult <object> queryResult = instance.Bucket.Query <object>(string.Format("DELETE FROM {0} WHERE POSITION(META().id, \"{1}\") = 0", SafeBucket.Instance.Bucket.Name, KeyGen.AttachPrefixToKey(keyPrefix)));
                    instance.Bucket.Query <object>(string.Format("DROP INDEX {0}.{1}_{2}_KeyPrefix_Index USING GSI", SafeBucket.Instance.Bucket.Name, AppContext.AppKey, keyPrefix));
                    DebugUtil.CollectDebugInfo(string.Format("Reset keys by prefix('{0}'), Status:{1}, Exception:{2}", keyPrefix, queryResult.Status, (queryResult.Exception == null ? "null" : queryResult.Exception.Message)));
                    cacheResult1 = new CacheResult(queryResult);
                }
                finally
                {
                    if (instance != null)
                    {
                        ((IDisposable)instance).Dispose();
                    }
                }
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                cacheResult1.Exception = exception;
                DebugUtil.CollectDebugInfo(string.Format("Reset keys by prefix('{0}'), Exception:{1}", keyPrefix, exception.Message));
            }
            return(cacheResult1);
        }
Exemplo n.º 8
0
        public ICacheResult <T> Get <T>(string key)
        {
            CacheResult <T> cacheResult = new CacheResult <T>()
            {
                Success = false,
                Status  = Status.None
            };
            CacheResult <T> cacheResult1 = cacheResult;

            try
            {
                SafeBucket instance = SafeBucket.Instance;
                try
                {
                    IOperationResult <T> operationResult = instance.Bucket.Get <T>(KeyGen.AttachPrefixToKey(key));
                    object[]             objArray        = new object[] { key, operationResult.Status, null, null };
                    objArray[2] = (operationResult.Value == null ? "null" : operationResult.Value.ToString());
                    objArray[3] = (operationResult.Exception == null ? "null" : operationResult.Exception.Message);
                    DebugUtil.CollectDebugInfo(string.Format("Get<T>('{0}'), Status:{1}, Value:{2}, Exception:{3}", objArray));
                    cacheResult1 = new CacheResult <T>(operationResult);
                }
                finally
                {
                    if (instance != null)
                    {
                        ((IDisposable)instance).Dispose();
                    }
                }
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                cacheResult1.Exception = exception;
                DebugUtil.CollectDebugInfo(string.Format("Get<T>('{0}'), Exception:{1}", key, exception.Message));
            }
            return(cacheResult1);
        }