コード例 #1
0
        protected virtual RedisCachedObject <T> GetCachedValue <T>(RedisValue redisValue, IRedisService service, bool first)
        {
            string decompressed = null;

            if (default(RedisValue) == redisValue)
            {
                return(CreateCachedValue <T>());
            }
            if (CacheSettings.ServiceSettings.CompressValues)
            {
                decompressed = DecompressValue(redisValue);
            }
            if (default(RedisValue) == redisValue)
            {
                return(CreateCachedValue <T>());
            }
            decompressed = decompressed ?? redisValue;
            RedisCachedObject <T> value;

            if (!first)
            {
                ISerializationService sz = CacheSettings.SerializationService;
                try
                {
                    return(sz.DeserializeObject <RedisCachedObject <T> >(decompressed, CacheSettings.SerializationSettings));
                }
                catch (SerializationException)
                {
                    return(CreateCachedValue <T>());
                }
            }

            RedisCachedObject <string> stringValue = GetCachedValue <string>(redisValue, service, false);

            if (typeof(T) == typeof(object) || typeof(T) == typeof(string))
            {
                value = ChangeType <T, string>(stringValue);
            }
            else if (typeof(T).IsPrimitive)
            {
                value = ChangeType <T, string>(stringValue);
            }
            else
            {
                ISerializationService sz = CacheSettings.SerializationService;
                value = new RedisCachedObject <T>
                {
                    RetrievedSuccesfully = stringValue.RetrievedSuccesfully,
                    Value      = sz.DeserializeObject <T>(stringValue.Value, CacheSettings.SerializationSettings),
                    CachedTime = stringValue.CachedTime,
                    Status     = stringValue.Status,
                    Metadata   = stringValue.Metadata,
                    Id         = stringValue.Id,
                    ExpireTime = stringValue.ExpireTime,
                };
            }

            if (value != null && value.ExpireTime < DateTime.UtcNow)
            {
                if (value.Id != null)
                {
                    service.DeleteValue(value.Id);
                }
                return(CreateCachedValue <T>());
            }

            return(value);
        }