public async Task <CachedObject <TResult> > GetAsync <TResult>(ProxiedMethodInvocation <T, TResult> proxiedMethodInvocation, CachePolicy cachePolicy)
        {
            return(await Task.Factory.StartNew(() =>
            {
                var hash = proxiedMethodInvocation.GetHashString();
                DictionaryCacheItem cachedObject;
                if (_cache.TryGetValue(hash, out cachedObject))
                {
                    if (cachedObject.ThrownException != null && cachedObject.Created.AddSeconds(cachePolicy.ExceptionCacheDuration) > DateTime.Now)
                    {
                        return new CachedObject <TResult>(CachedObjectState.Exception, cachedObject.ThrownException);
                    }
                    else if (cachedObject.ThrownException != null)
                    {
                        return new CachedObject <TResult>(CachedObjectState.None, null);
                    }

                    if (cachedObject.AbsoluteDuration.TotalSeconds > 0 && cachedObject.Created + cachedObject.AbsoluteDuration < DateTime.Now)
                    {
                        return new CachedObject <TResult>(CachedObjectState.None, null);
                    }

                    return new CachedObject <TResult>(cachedObject.IsExpired ? CachedObjectState.Stale : CachedObjectState.Fresh, (TResult)cachedObject.Object);
                }
                return CachedObject <TResult> .Empty();
            }));
        }
        public async Task StoreExceptionAsync <TResult>(ProxiedMethodInvocation <T, TResult> proxiedMethodInvocation, CachePolicy cachePolicy, Exception e)
        {
            await Task.Factory.StartNew(() =>
            {
                var hash             = proxiedMethodInvocation.GetHashString();
                var duration         = TimeSpan.FromSeconds(cachePolicy.CacheDuration);
                var absoluteDuration = TimeSpan.FromSeconds(cachePolicy.MaxAge);

                _cache[hash] = new DictionaryCacheItem(e, duration, absoluteDuration);
            });
        }
Ejemplo n.º 3
0
        public async Task <CachedObject <TResult> > GetAsync <TResult>(ProxiedMethodInvocation <T, TResult> proxiedMethodInvocation, CachePolicy cachePolicy)
        {
            var key           = proxiedMethodInvocation.GetHashString(_hashScramble);
            var memcachedItem = await _memcachedCluster.Get(key);

            if (memcachedItem != null)
            {
                MemcachedObject <TResult> cacheItem;
                using (var zipStream = new GZipStream(memcachedItem.Data, CompressionMode.Decompress))
                {
                    using (var streamReader = new StreamReader(zipStream))
                    {
                        var textReader = new JsonTextReader(streamReader);
                        try
                        {
                            cacheItem = _serializer.Deserialize <MemcachedObject <TResult> >(textReader);
                        }
                        catch (JsonSerializationException)
                        {
                            //This exception occurs if whatever is in memcached 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)));
        }
Ejemplo n.º 4
0
        public async Task StoreAsync <TResult>(ProxiedMethodInvocation <T, TResult> proxiedMethodInvocation, CachePolicy cachePolicy, TResult data)
        {
            var key          = proxiedMethodInvocation.GetHashString(_hashScramble);
            var cachedObject = new MemcachedObject <TResult>()
            {
                Created = DateTime.Now,
                Object  = data
            };

            var bytes = SerializeAndZip(cachedObject);

            if (cachePolicy.MaxAge > 0)
            {
                await _memcachedCluster.Set(key, bytes, new MemcachedStorageOptions()
                {
                    ExpirationTime = TimeSpan.FromSeconds(cachePolicy.MaxAge)
                });
            }
            else
            {
                await _memcachedCluster.Set(key, bytes);
            }
        }