Пример #1
0
        public ItemCache <T> GetItemCache <T>(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentException("Parameter is invalid.", "key", null);
            }

            RedisDal dal = new RedisDal();

            StackExchange.Redis.RedisValue[] results = dal.GetListItem <T>(key);

            if (results != null && results.Length > 1)
            {
                ItemCacheInfo <T> itemCacheInfo = new ItemCacheInfo <T>();
                itemCacheInfo.Serialized_TTL  = (string)results[0];
                itemCacheInfo.Serialized_Data = results[1];
                itemCacheInfo.DeSerializeInfo();

                if (Utility.TTL_Is_Expired(itemCacheInfo.SlidingExpiration_DT, itemCacheInfo.AbsoluteExpiration_DT))
                {
                    dal.ItemDelete(key);
                    return(null);
                }
                else
                {
                    //Update SLI TTL on Redis...
                    if (itemCacheInfo.SlidingExpiration_DT != DateTime.MaxValue)
                    {
                        itemCacheInfo.UpdateSerialized_TTL();       //Update TTL

                        dal.UpdateTTL_Item(key, itemCacheInfo.Serialized_TTL);
                        dal.SetTTL(key, itemCacheInfo.SlidingExpiration_TS);
                    }

                    //Prepare ItemCache Result...
                    ItemCache <T> result = new ItemCache <T>();
                    result.SlidingExpiration  = itemCacheInfo.SlidingExpiration_TS;
                    result.AbsoluteExpiration = itemCacheInfo.AbsoluteExpiration_TS;
                    result.Key   = key;
                    result.Value = itemCacheInfo.Data;
                    return(result);
                }
            }
            else
            {
                return(null);
            }
        }
Пример #2
0
        public long Add <T>(string key, T value, TimeSpan slidingExpiration, TimeSpan absoluteExpiration, bool forceOverWrite)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentException("Parameter is invalid.", "key", null);
            }
            if ((slidingExpiration != Utility.NO_EXPIRATION && absoluteExpiration != Utility.NO_EXPIRATION) && (slidingExpiration >= absoluteExpiration))
            {
                throw new RedisCacheException("Sliding Expiration is greater or equal than Absolute Expiration.", null);
            }

            RedisDal dal = new RedisDal();

            if (_TypeStorage == Utility.TypeStorage.UseList)
            {
                if (!forceOverWrite)
                {
                    if (dal.ItemExist(key))
                    {
                        throw new RedisCacheException("This Item Exists.", null);
                    }
                    else
                    {
                        //Continue...
                    }
                }
                else
                {
                    if (dal.ItemExist(key))
                    {
                        dal.ItemDelete(key);
                    }
                    else
                    {
                        //Continue...
                    }
                }

                ItemCacheInfo <T> itemCacheInfo = new ItemCacheInfo <T>();
                itemCacheInfo.Data = value;
                itemCacheInfo.AbsoluteExpiration_TS = absoluteExpiration;
                itemCacheInfo.SlidingExpiration_TS  = slidingExpiration;
                itemCacheInfo.SerializeInfo();

                long result = dal.AddListItem(key, itemCacheInfo.Serialized_Data, itemCacheInfo.Serialized_TTL);
                _SetTTL(key, slidingExpiration, absoluteExpiration, dal);
                return(result);
            }
            else if (_TypeStorage == Utility.TypeStorage.UseHash)
            {
                throw new System.NotImplementedException();
            }
            if (_TypeStorage == Utility.TypeStorage.UseKeyValue)
            {
                throw new System.NotImplementedException();
            }
            else
            {
                throw new System.NotImplementedException();
            }
        }