Пример #1
0
        public async Task <Model.CachedObject <TResult> > GetAsync <TResult>(Core.Util.ProxiedMethodInvocation <T, TResult> proxiedMethodInvocation, CachePolicy cachePolicy)
        {
            var key  = proxiedMethodInvocation.GetHashString(_hashScramble);
            var data = await _redis.Get(key);

            if (data != null)
            {
                RedisObject <TResult> cacheItem;
                using (var dataStream = new MemoryStream(data))
                {
                    using (var zipStream = new GZipStream(dataStream, CompressionMode.Decompress))
                    {
                        using (var streamReader = new StreamReader(zipStream))
                        {
                            var textReader = new JsonTextReader(streamReader);
                            try
                            {
                                cacheItem = _serializer.Deserialize <RedisObject <TResult> >(textReader);
                            }
                            catch (JsonSerializationException)
                            {
                                //This exception occurs if whatever is in Redis is impossible to deserialize. It's a tricky case, but we'll have to report back that nothing is in there.
                                return(new CachedObject <TResult>(CachedObjectState.None, default(TResult)));
                            }
                        }
                    }
                }

                if (cacheItem == null)
                {
                    return(new CachedObject <TResult>(CachedObjectState.None, default(TResult)));
                }

                if (cacheItem.IsException && cacheItem.Created.AddSeconds(cachePolicy.ExceptionCacheDuration) > DateTime.Now)
                {
                    return(new CachedObject <TResult>(CachedObjectState.Exception, cacheItem.Exception));
                }
                else if (cacheItem.IsException)
                {
                    return(new CachedObject <TResult>(CachedObjectState.None, default(TResult)));
                }

                var fresh = cacheItem.Created.AddSeconds(cachePolicy.CacheDuration) > DateTime.Now;
                var state = fresh ? CachedObjectState.Fresh : CachedObjectState.Stale;

                if (cachePolicy.DiscardStale && state == CachedObjectState.Stale)
                {
                    return(new CachedObject <TResult>(CachedObjectState.None, default(TResult)));
                }

                return(new CachedObject <TResult>(state, cacheItem.Object));
            }

            return(new CachedObject <TResult>(CachedObjectState.None, default(TResult)));
        }
Пример #2
0
        public async Task StoreAsync <TResult>(Core.Util.ProxiedMethodInvocation <T, TResult> proxiedMethodInvocation, CachePolicy cachePolicy, TResult data)
        {
            var key          = proxiedMethodInvocation.GetHashString(_hashScramble);
            var cachedObject = new RedisObject <TResult>()
            {
                Created = DateTime.Now,
                Object  = data
            };

            var bytes = SerializeAndZip(cachedObject);

            if (cachePolicy.MaxAge > 0)
            {
                await _redis.Set(key, bytes, TimeSpan.FromSeconds(cachePolicy.MaxAge));
            }
            else
            {
                await _redis.Set(key, bytes);
            }
        }