public bool Put(TemplateCacheKey key, T templateInfo, TimeSpan slidingExpiration)
        {
            var item = new LocalTemplateCacheItem <T>(new TemplateCacheItem <T>(key, templateInfo), slidingExpiration);

            _cache[key] = item;
            return(true);
        }
        public TemplateCacheItem <T> Get(TemplateCacheKey key)
        {
            var cacheKey = string.Format(CacheKeyTemplate, typeof(T), key);
            var item     = MemoryCache.Default.Get(cacheKey) as TemplateCacheItem <T>;

            return(item);
        }
Ejemplo n.º 3
0
        public TemplateCacheItem(TemplateCacheKey key, T templateInfo)
        {
            Contract.Requires(key != null);

            _key          = key;
            _templateInfo = templateInfo;
        }
        public bool Put(TemplateCacheKey key, T templateInfo, TimeSpan slidingExpiration)
        {
            var cacheKey = string.Format(CacheKeyTemplate, typeof(T), key);

            return(MemoryCache.Default.Add(cacheKey, new TemplateCacheItem <T>(key, templateInfo),
                                           new CacheItemPolicy {
                SlidingExpiration = slidingExpiration
            }));
        }
        public TemplateCacheItem <T> Get(TemplateCacheKey key)
        {
            var item = EnsureExpiration(key);

            if (item == null)
            {
                return(null);
            }
            item.LastAccessTime = DateTime.UtcNow;
            return(item.Item);
        }
        private LocalTemplateCacheItem <T> EnsureExpiration(TemplateCacheKey key)
        {
            if (!_cache.ContainsKey(key))
            {
                return(null);
            }
            var item = _cache[key];

            if (item.LastAccessTime + item.SlidingExpiration < DateTime.UtcNow)
            {
                _cache.TryRemove(key, out item);
                return(null);
            }
            return(item);
        }
        public bool ContainsKey(TemplateCacheKey key)
        {
            var cacheKey = string.Format(CacheKeyTemplate, typeof(T), key);

            return(MemoryCache.Default.Contains(cacheKey, null));
        }
        public bool ContainsKey(TemplateCacheKey key)
        {
            var item = EnsureExpiration(key);

            return(item != null);
        }