コード例 #1
0
ファイル: RedisCache.cs プロジェクト: zszqwe/CommonX
 public object Get(string key)
 {
     if (IsSuspended)
     {
         return(null);
     }
     if (_redis != null)
     {
         try
         {
             CacheUtility.EnsureCacheKey(ref key);
             var redisValue = _redis.HashGet(CacheName, key);
             ///if (string.IsNullOrEmpty(redisValue)) return null;
             if (!redisValue.HasValue)
             {
                 return(null);
             }
             var data = _binarySerializer.Deserialize <object>(redisValue);
             ResetCacheStatus();
             return(data);
         }
         catch (Exception ex)
         {
             CatchCriticalError("GetData", _cacheName, key, ex);
         }
     }
     return(null);
 }
コード例 #2
0
ファイル: RedisCache.cs プロジェクト: zszqwe/CommonX
        public T GetAndRemove <T>(string key)
        {
            var data = default(T);

            if (IsSuspended)
            {
                return(data);
            }

            if (_redis != null)
            {
                try
                {
                    CacheUtility.EnsureCacheKey(ref key);
                    data = (T)_binarySerializer.Deserialize(_redis.HashGet(CacheName, key), typeof(T));

                    if (data != null)
                    {
                        _redis.HashDelete(CacheName, key);
                        ResetCacheStatus();
                    }
                }
                catch (Exception ex)
                {
                    CatchCriticalError("GetAndRemove", _cacheName, key, ex);
                }
            }
            return(data);
        }
コード例 #3
0
ファイル: RedisCache.cs プロジェクト: zszqwe/CommonX
 public void RemoveItemsFromList <T>(string cacheKey, List <T> entitys)
 {
     if (_redis != null)
     {
         try
         {
             CacheUtility.EnsureCacheKey(ref cacheKey);
             lock (SyncRoot3)
             {
                 var list    = _binarySerializer.Deserialize <List <T> >(_redis.HashGet(CacheName, cacheKey));
                 var newGuys = new List <T>();
                 newGuys.AddRange(list);
                 foreach (object o in entitys)
                 {
                     foreach (var oneT in list)
                     {
                         if (o.Equals(oneT))
                         {
                             newGuys.Remove(oneT);
                         }
                     }
                 }
                 _redis.HashSet(CacheName, cacheKey, _binarySerializer.Serialize(newGuys));
             }
             ResetCacheStatus();
         }
         catch (Exception ex)
         {
             CatchCriticalError("RemvoeItemsFromList", _cacheName, cacheKey, ex);
         }
     }
 }
コード例 #4
0
ファイル: RedisCache.cs プロジェクト: zszqwe/CommonX
        public void Add(string key, object data)
        {
            if (IsSuspended)
            {
                return;
            }
            if (_redis != null)
            {
                var retryTimes = 10;
                var counter    = 0;
                //var m = Measure.StartNew();
                CacheUtility.EnsureCacheKey(ref key);
                while (retryTimes > 0)
                {
                    counter++;
                    try
                    {
                        _redis.HashSet(CacheName, key, _binarySerializer.Serialize(data));

                        ResetCacheStatus();
                        retryTimes = 0;
                        //m.LogDuration(counter + "Added ");
                    }
                    catch (Exception ex)
                    {
                        retryTimes = 0;
                        CatchCriticalError("Add", _cacheName, key, ex);
                    }
                }
            }
        }
コード例 #5
0
ファイル: DefaultMemoryCache.cs プロジェクト: zszqwe/CommonX
        public T Get<T>(string key)
        {
            CacheUtility.EnsureCacheKey(ref key);
            var dataObj = Get(key);

            return dataObj == null ? default(T) : (T)dataObj;
        }
コード例 #6
0
ファイル: RedisCache.cs プロジェクト: zszqwe/CommonX
        public List <T> GetAndRemoveList <T>(string key)
        {
            List <T> list = null;

            if (_redis != null)
            {
                CacheUtility.EnsureCacheKey(ref key);
                var retryTimes = 100;
                //var m = Measure.StartNew();
                while (retryTimes > 0)
                {
                    try
                    {
                        lock (SyncRoot2)
                        {
                            list = _binarySerializer.Deserialize <List <T> >(_redis.HashGet(CacheName, key));
                            _redis.HashDelete(CacheName, key);
                        }
                        retryTimes = 0;
                        //m.LogDuration("GetAndLock/Remove " + key);
                    }
                    catch (Exception ex)
                    {
                        CatchCriticalError("GetAndRemove", _cacheName, key, ex);
                        retryTimes = 0;
                    }
                }
            }
            return(list);
        }
コード例 #7
0
ファイル: RedisCache.cs プロジェクト: zszqwe/CommonX
 public object this[string key]
 {
     get
     {
         CacheUtility.EnsureCacheKey(ref key);
         return(Get(key));
     }
 }
コード例 #8
0
ファイル: DefaultMemoryCache.cs プロジェクト: zszqwe/CommonX
        public object Get(string key)
        {
            CacheUtility.EnsureCacheKey(ref key);

            var dataObj = DataCache.Get(key);

            return dataObj == null ? null : _binarySerializer.Deserialize<object>(dataObj as byte[]);
        }
コード例 #9
0
ファイル: RedisCache.cs プロジェクト: zszqwe/CommonX
        public bool AddToList <T>(string key, T data, int maxSize = 0)
        {
            var retVal = true;

            if (_redis != null)
            {
                CacheUtility.EnsureCacheKey(ref key);
                var MAX_COUNT  = 100;
                var retryTimes = MAX_COUNT;
                while (retryTimes > 0)
                {
                    //var m = Measure.StartNew();
                    try
                    {
                        lock (SyncRoot)
                        {
                            var list = _binarySerializer.Deserialize <List <T> >(_redis.HashGet(CacheName, key));
                            if (maxSize > 0 && list.Count > maxSize)
                            {
                                retVal = false;
                            }
                            else
                            {
                                list.Add(data);
                            }
                            _redis.HashSet(CacheName, key, _binarySerializer.Serialize(list));
                        }
                        retryTimes = 0;
                        //m.LogDuration("GetAndLock/PutAndUnlock " + key);
                    }
                    catch (Exception ex)
                    {
                        //m.LogDuration("retry - GetAndLock/PutAndUnlock exception " + key);
                        retryTimes = 0;
                        CatchCriticalError("AddToList-GetandPut-retry", _cacheName, key, ex);
                    }
                }
            }
            else
            {
                _logger.Debug("!!! DataCache is NULL!");
            }
            return(retVal);
        }
コード例 #10
0
ファイル: RedisCache.cs プロジェクト: zszqwe/CommonX
        public void Remove(string key)
        {
            if (IsSuspended)
            {
                return;
            }

            if (_redis != null)
            {
                try
                {
                    CacheUtility.EnsureCacheKey(ref key);
                    _redis.HashDelete(CacheName, key);
                    ResetCacheStatus();
                }
                catch (Exception ex)
                {
                    CatchCriticalError("Remove", _cacheName, key, ex);
                }
            }
        }
コード例 #11
0
ファイル: RedisCache.cs プロジェクト: zszqwe/CommonX
        public void Put(string key, object data)
        {
            if (IsSuspended)
            {
                return;
            }

            if (_redis != null)
            {
                try
                {
                    CacheUtility.EnsureCacheKey(ref key);
                    _redis.HashSet(CacheName, key, _binarySerializer.Serialize(data));
                    ResetCacheStatus();
                }
                catch (Exception ex)
                {
                    CatchCriticalError("Put", _cacheName, key, ex);
                }
            }
        }
コード例 #12
0
ファイル: DefaultMemoryCache.cs プロジェクト: zszqwe/CommonX
        public void Add(string key, object value)
        {
            if (value == null)
                return;

            //var config = CacheConfiguration.Instance.CacheConfigurations.FirstOrDefault(a => TypeUtils.GetFormattedName(a.Name).Equals(_cacheName));
            var config = _cacheConfiguration.Instance.CacheConfigurations.FirstOrDefault(a => TypeUtils.GetFormattedName(a.Name).Equals(_cacheName));

            if (config != null)
            {
                _policy.AbsoluteExpiration = DateTime.Now.AddMilliseconds(config.DefaultTimeOut);
            }
            if (_policy.AbsoluteExpiration <= DateTimeOffset.Now)
                return;
            CacheUtility.EnsureCacheKey(ref key);
            var cacheItem = new CacheItem(key, _binarySerializer.Serialize(value));

            DataCache.Add(cacheItem, _policy);

            var d = new CacheStatus(key, DateTime.Now, _policy.AbsoluteExpiration);
            CacheStatus.AddOrUpdate(key, d, (k, j) => { return d; });
        }
コード例 #13
0
ファイル: RedisCache.cs プロジェクト: zszqwe/CommonX
 public bool Contains(string key)
 {
     CacheUtility.EnsureCacheKey(ref key);
     return(Get(key) != null);
 }
コード例 #14
0
ファイル: DefaultMemoryCache.cs プロジェクト: zszqwe/CommonX
        public void Remove(string key)
        {
            CacheUtility.EnsureCacheKey(ref key);
            DataCache.Remove(key);

        }
コード例 #15
0
ファイル: DefaultMemoryCache.cs プロジェクト: zszqwe/CommonX
 public bool Contains(string key)
 {
     CacheUtility.EnsureCacheKey(ref key);
     return DataCache.Contains(key);
 }
コード例 #16
0
ファイル: DefaultMemoryCache.cs プロジェクト: zszqwe/CommonX
 public void Put(string key, object value)
 {
     CacheUtility.EnsureCacheKey(ref key);
     DataCache.Remove(key);
     Add(key, value);
 }