| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 | using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
namespace iiie.CacheControl.Business.OutputCache
{
    public class MemoryOutputCache : IOutputCache
    {
        private static readonly MemoryCache Cache = MemoryCache.Default;
        public void RemoveStartsWith(string key)
        {
            lock (Cache)
            {
                Cache.Remove(key);
            }
        }
        public T Get<T>(string key) where T : class
        {
            var o = Cache.Get(key) as T;
            return o;
        }
        public object Get(string key)
        {
            return Cache.Get(key);
        }
        public void Remove(string key)
        {
            lock (Cache)
            {
                Cache.Remove(key);
            }
        }
        public bool Contains(string key)
        {
            return Cache.Contains(key);
        }
        public void Add(string key, object o, string dependsOnKey = null)
        {
            var cachePolicy = new CacheItemPolicy();
            if (!string.IsNullOrWhiteSpace(dependsOnKey))
            {
                cachePolicy.ChangeMonitors.Add(
                    Cache.CreateCacheEntryChangeMonitor(new[] { dependsOnKey })
                );
            }
            lock (Cache)
            {
                Cache.Add(key, o, cachePolicy);
            }
        }
        public IEnumerable<string> AllKeys
        {
            get
            {
                return Cache.Select(x => x.Key);
            }
        }
    }
}
 |