Exemplo n.º 1
0
        public T UsingKeys(params object[] keys)
        {
            var key      = new MemCacheKey(typeof(T), keys);
            var lazyFunc = new Lazy <T>(_func);

            return(_queryCache.GetValue(key, lazyFunc));
        }
Exemplo n.º 2
0
        public T GetOrAdd <T>(Expression <Func <T> > expression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            object[] parameters = _expressionValueExtractor.GetArgumentValues(expression);
            var      key        = new MemCacheKey(typeof(T), parameters);

            Func <T> funcToExecute    = expression.Compile();
            var      newLazyCacheItem = new Lazy <T>(funcToExecute);

            return(GetValue(key, newLazyCacheItem));
        }
Exemplo n.º 3
0
        internal T GetValue <T>(MemCacheKey key, Lazy <T> newLazyCacheItem)
        {
            var existingCacheItem = (Lazy <T>)_cache.AddOrGetExisting(key.ToString(), newLazyCacheItem, new CacheItemPolicy());

            if (existingCacheItem != null)
            {
                return(existingCacheItem.Value);
            }

            try
            {
                return(newLazyCacheItem.Value);
            }
            catch // the provided function failed, so remove it from cache
            {
                _cache.Remove(key.ToString());
                throw;
            }
        }