Exemple #1
0
 protected override ICacheHolder <TKey, TValue> GetCacheHolder <TKey, TValue>(CachedFuncOptions options)
 {
     if (options != null)
     {
         return(new MemoryCacheHolder <TKey, TValue>(MemoryCache.Default, options));
     }
     return(base.GetCacheHolder <TKey, TValue>(null));
 }
Exemple #2
0
 public MemoryCacheHolder(IMemoryCache cache, CachedFuncOptions options)
 {
     _cache   = cache;
     _options = new MemoryCacheEntryOptions
     {
         AbsoluteExpiration = options.AbsoluteExpiration,
         AbsoluteExpirationRelativeToNow = options.AbsoluteExpirationRelativeToNow,
         SlidingExpiration = options.SlidingExpiration
     };
 }
        public MemoryCacheHolder(ObjectCache cache, CachedFuncOptions options)
        {
            _cache = cache;

            if (options.AbsoluteExpirationRelativeToNow != null)
            {
                if (options.AbsoluteExpiration != null)
                {
                    _optionsFactory = () =>
                    {
                        DateTimeOffset abs = DateTimeOffset.Now.Add((TimeSpan)options.AbsoluteExpirationRelativeToNow);
                        if (abs > options.AbsoluteExpiration)
                        {
                            abs = (DateTimeOffset)options.AbsoluteExpiration;
                        }
                        var ret = new CacheItemPolicy
                        {
                            AbsoluteExpiration = abs,
                            SlidingExpiration  = options.SlidingExpiration ?? default(TimeSpan)
                        };
                        return(ret);
                    };
                    return;
                }
                _optionsFactory = () => new CacheItemPolicy
                {
                    AbsoluteExpiration = DateTimeOffset.Now.Add((TimeSpan)options.AbsoluteExpirationRelativeToNow),
                    SlidingExpiration  = options.SlidingExpiration ?? default(TimeSpan)
                };
                return;
            }
            CacheItemPolicy po = new CacheItemPolicy
            {
                AbsoluteExpiration = options.AbsoluteExpiration ?? DateTimeOffset.MaxValue,
                SlidingExpiration  = options.SlidingExpiration ?? default(TimeSpan)
            };

            _optionsFactory = () => po;
        }