Exemple #1
0
 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);
 }
Exemple #2
0
        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);
        }
Exemple #3
0
 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);
         }
     }
 }
Exemple #4
0
        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);
                    }
                }
            }
        }
Exemple #5
0
        public T Get<T>(string key)
        {
            CacheUtility.EnsureCacheKey(ref key);
            var dataObj = Get(key);

            return dataObj == null ? default(T) : (T)dataObj;
        }
Exemple #6
0
        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);
        }
Exemple #7
0
 public object this[string key]
 {
     get
     {
         CacheUtility.EnsureCacheKey(ref key);
         return(Get(key));
     }
 }
Exemple #8
0
        public object Get(string key)
        {
            CacheUtility.EnsureCacheKey(ref key);

            var dataObj = DataCache.Get(key);

            return dataObj == null ? null : _binarySerializer.Deserialize<object>(dataObj as byte[]);
        }
Exemple #9
0
        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);
        }
Exemple #10
0
        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);
                }
            }
        }
Exemple #11
0
        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);
                }
            }
        }
Exemple #12
0
        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; });
        }
Exemple #13
0
 public bool Contains(string key)
 {
     CacheUtility.EnsureCacheKey(ref key);
     return(Get(key) != null);
 }
Exemple #14
0
        public void Remove(string key)
        {
            CacheUtility.EnsureCacheKey(ref key);
            DataCache.Remove(key);

        }
Exemple #15
0
 public bool Contains(string key)
 {
     CacheUtility.EnsureCacheKey(ref key);
     return DataCache.Contains(key);
 }
Exemple #16
0
 public void Put(string key, object value)
 {
     CacheUtility.EnsureCacheKey(ref key);
     DataCache.Remove(key);
     Add(key, value);
 }