Looking for .net Keywords? Try Ask4Keywords

.NET Framework System.Runtime.Caching.MemoryCache (ObjectCache)


Эта функция получает существующий кеш формы элемента, и если элемент не существует в кеше, он будет извлекать элемент на основе функции valueFetchFactory.

    public static TValue GetExistingOrAdd<TValue>(string key, double minutesForExpiration, Func<TValue> valueFetchFactory)
            //The Lazy class provides Lazy initialization which will evaluate 
            //the valueFetchFactory only if item is not in the cache.
            var newValue = new Lazy<TValue>(valueFetchFactory);

            //Setup the cache policy if item will be saved back to cache.
            CacheItemPolicy policy = new CacheItemPolicy()
                AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(minutesForExpiration)

            //returns existing item form cache or add the new value if it does not exist.
            var cachedItem = _cacheContainer.AddOrGetExisting(key, newValue, policy) as Lazy<TValue>;

            return (cachedItem ?? newValue).Value;
        catch (Exception excep)
            return default(TValue);