using System; using System.Collections.Specialized; using System.Web.Caching; using Sony.Filtr.Bootstrap; using Sony.Filtr.DistributedCaching; using Sony.Filtr.ErrorLogging; namespace Sony.Filtr.API.WCF { public class DistributedOutputCacheProvider : OutputCacheProvider { private DistributedCacheHandler _distributedCacheHandler; public override void Initialize(string name, NameValueCollection config) { try { var ioc = new Bootstrapper().InitializeAsync().Result; _distributedCacheHandler = ioc.GetInstance(); } catch(Exception ex) { ErrorLoggingManager.Instance.LogError(ex); } base.Initialize(name, config); } public override object Add(string key, object entry, DateTime utcExpiry) { if (_distributedCacheHandler == null) { return null; } var cacheKey = GetKey(key); var obj = _distributedCacheHandler.Get(cacheKey); if (obj == null) _distributedCacheHandler.Put(cacheKey, entry, utcExpiry); return obj; } public override object Get(string key) { if (_distributedCacheHandler == null) { return null; } var cacheKey = GetKey(key); return _distributedCacheHandler.Get(cacheKey); } public override void Remove(string key) { if (_distributedCacheHandler == null) { return; } var cacheKey = GetKey(key); _distributedCacheHandler.Remove(cacheKey); } public override void Set(string key, object entry, DateTime utcExpiry) { if (_distributedCacheHandler == null) { return; } var cacheKey = GetKey(key); _distributedCacheHandler.Put(cacheKey, entry, utcExpiry); } private static string GetKey(string key) { return "OutputCache-1.1" + key; } } }