Ejemplo n.º 1
0
        public T GetOrSet <T>(string key, Func <T> dataGetter, DateTimeOffset absoluteExpirationOffset)
        {
            CachedItem <T> data = MemoryCache.Default.Get(key) as CachedItem <T>;

            if (data == null)
            {
                data = new CachedItem <T> {
                    Item = dataGetter()
                };

                var policy = new CacheItemPolicy {
                    AbsoluteExpiration = absoluteExpirationOffset
                };

                MemoryCache.Default.Add(key, data, policy);
            }
            return(data.Item);
        }
Ejemplo n.º 2
0
        public async Task <T> GetOrSetAsync <T>(string key, Func <Task <T> > dataGetter, TimeSpan slidingExpirationInterval)
        {
            CachedItem <T> data = MemoryCache.Default.Get(key) as CachedItem <T>;

            if (data == null)
            {
                data = new CachedItem <T>()
                {
                    Item = await dataGetter()
                };

                var policy = new CacheItemPolicy {
                    SlidingExpiration = slidingExpirationInterval
                };

                MemoryCache.Default.Add(key, data, policy);
            }
            return(data.Item);
        }