Пример #1
0
        public static async Task <T> GetOrAddAsync <T>(Func <Task <T> > builder, TimeSpan expiresIn, params string[] keys)
        {
            var key = GetKey(keys);
            CacheItemWrapper itemWrapper = null;
            var cahedItem = MemoryCache.Default.Get(key);

            if (cahedItem != null)
            {
                itemWrapper = (CacheItemWrapper)cahedItem;
            }

            if (itemWrapper == null || DateTime.Now.Subtract(itemWrapper.InsertedAt).TotalSeconds >= expiresIn.TotalSeconds)
            {
                ////expired or not in cache
                try
                {
                    var item = await builder();

                    var wrapper = new CacheItemWrapper
                    {
                        InsertedAt = DateTime.Now,
                        Item       = item
                    };
                    MemoryCache.Default.Add(key, wrapper, DateTime.Now.AddSeconds(expiresIn.TotalSeconds));
                    return(item);
                }
                catch (AggregateException ae)
                {
                    throw ae.InnerException;
                }
            }

            return((T)itemWrapper.Item);
        }
Пример #2
0
        public static T GetOrAdd <T>(Func <T> builder, TimeSpan expiresIn, params string[] keys)
        {
            var key = GetKey(keys);
            CacheItemWrapper itemWrapper = null;
            var cachedItem = MemoryCache.Default.Get(key);

            if (cachedItem != null)
            {
                itemWrapper = (CacheItemWrapper)cachedItem;
            }

            if (itemWrapper != null && !(DateTime.Now.Subtract(itemWrapper.InsertedAt).TotalSeconds >=
                                         expiresIn.TotalSeconds))
            {
                return((T)itemWrapper.Item);
            }

            ////expired or not in cache
            var item    = builder();
            var wrapper = new CacheItemWrapper
            {
                InsertedAt = DateTime.Now,
                Item       = item
            };

            MemoryCache.Default.Add(key, wrapper, DateTime.Now.AddSeconds(expiresIn.TotalSeconds));
            return(item);
        }
        public async Task <T> WrapToCacheAsync <T>(IInvocation invocation, TimeSpan?duration)
        {
            var cacheKey = BuildCacheKeyFrom(invocation);

            //try get the return value from the cache provider
            var cachedValue = _cacheManager.Get <CacheItemWrapper <T> >(cacheKey);

            if (cachedValue != null)
            {
                return(await Task.FromResult(cachedValue.Item).ConfigureAwait(false));
            }

            //call the intercepted method
            invocation.Proceed();

            var resultTask     = (Task <T>)invocation.ReturnValue;
            var setInCacheTask = resultTask.ContinueWith(t =>
            {
                var result = new CacheItemWrapper <T>(t.Result);
                if (!t.IsFaulted && t.Result != null)
                {
                    _cacheManager.Set(cacheKey, result, duration ?? _cacheExpiration.Timeout);
                }
            });
            await setInCacheTask;

            return(await resultTask.ConfigureAwait(false));
        }
Пример #4
0
        public static void Add(object item, string key)
        {
            var wrapper = new CacheItemWrapper
            {
                InsertedAt = DateTime.Now,
                Item       = item
            };

            MemoryCache.Default.Add(key, wrapper, ObjectCache.InfiniteAbsoluteExpiration);
        }
        public T WrapToCache <T>(IInvocation invocation, TimeSpan?duration)
        {
            var cacheKey = BuildCacheKeyFrom(invocation);

            //try get the return value from the cache provider
            var cachedValue = _cacheManager.Get <CacheItemWrapper <T> >(cacheKey);

            if (cachedValue != null)
            {
                return(cachedValue.Item);
            }

            //call the intercepted method
            invocation.Proceed();

            if (invocation.ReturnValue == null)
            {
                return(default(T));
            }
            var cachedItem = new CacheItemWrapper <T>((T)invocation.ReturnValue);

            _cacheManager.Set(cacheKey, cachedItem, duration ?? _cacheExpiration.Timeout);
            return(cachedItem.Item);
        }