Created
January 8, 2016 11:47
-
-
Save carlwoodhouse/4c58b4ff2459669f2608 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text.RegularExpressions; | |
| using System.Web; | |
| namespace Patient.Framework.Caching { | |
| public class DefaultCacheProvider : ICacheProvider { | |
| string cacheStore = "FrameworkLocalStoreDefault"; | |
| public string CacheStoreKey { | |
| set { this.cacheStore = string.Format("FrameworkLocalStore{0}", value); } | |
| } | |
| public object Get<T>(string key) { | |
| LocalCacheObject cacheItem = null; | |
| if (GetLocalCache().TryGetValue(key, out cacheItem)) { | |
| if (cacheItem != null) { | |
| return cacheItem.Value != null ? (T)cacheItem.Value : default(T); | |
| } | |
| } | |
| return null; | |
| } | |
| public Dictionary<string, T> Get<T>(IEnumerable<string> keys) { | |
| var items = new Dictionary<string, T>(); | |
| var localCache = GetLocalCache(); | |
| foreach (var key in keys) { | |
| LocalCacheObject cacheItem = null; | |
| if (GetLocalCache().TryGetValue(key, out cacheItem)) { | |
| if (cacheItem != null) { | |
| items.Add(key, cacheItem.Value != null ? (T)cacheItem.Value : default(T)); | |
| } | |
| } | |
| } | |
| return items; | |
| } | |
| public void Put<T>(string key, T value) { | |
| this.Put<T>(key, value, TimeSpan.FromDays(365000)); | |
| } | |
| public void Put<T>(string key, T value, TimeSpan validFor) { | |
| var cache = GetLocalCache(); | |
| cache.Add(key, new LocalCacheObject(value, validFor)); | |
| HttpRuntime.Cache[cacheStore] = cache; | |
| } | |
| public void Put<T>(Dictionary<string, T> items, TimeSpan validFor) { | |
| if (items != null && items.Any()) { | |
| foreach (var key in items.Keys) { | |
| this.Put(key, items[key], validFor); | |
| } | |
| } | |
| } | |
| public void Evict(string key) { | |
| var cache = GetLocalCache(); | |
| cache.Remove(key); | |
| HttpRuntime.Cache[cacheStore] = cache; | |
| } | |
| public void EvictPattern(string keyPattern) { | |
| var keysToEvict = this.GetCacheItems(keyPattern).Select(x => x.Key); | |
| foreach (var key in keysToEvict) { | |
| this.Evict(key); | |
| } | |
| } | |
| public void EvictPrefix(string keyPrefix) { | |
| this.EvictPattern(keyPrefix); | |
| } | |
| public int KeyCount(string keyPattern) { | |
| return GetCacheItems(keyPattern).Count; | |
| } | |
| public IEnumerable<string> GetKeys(string keyPattern) { | |
| return GetCacheItems(keyPattern).Select(x => x.Key); | |
| } | |
| public void Tag(string tag, params string[] keys) { | |
| Put(tag, keys); | |
| } | |
| public IEnumerable<string> GetTaggedItems(string tag) { | |
| return (List<string>)Get<List<string>>(tag); | |
| } | |
| public void DeleteTag(string tag, CacheType? setLocalisation = null) { | |
| foreach (var key in GetTaggedItems(tag)) { | |
| Evict(key); | |
| } | |
| Evict(tag); | |
| } | |
| private Dictionary<string, object> GetCacheItems(string keyPattern) { | |
| var cacheItems = new Dictionary<string, object>(); | |
| var localCache = this.GetLocalCache(); | |
| var pattern = new Regex(Regex.Escape(keyPattern.Replace("*", "xxWILDCARDxx").Replace(".", string.Empty)) | |
| .Replace("xxWILDCARDxx", ".*"), RegexOptions.Compiled); | |
| foreach (var cacheKey in localCache.Keys) { | |
| var key = cacheKey.Replace(".", string.Empty); | |
| if (pattern.IsMatch(key)) { | |
| cacheItems.Add(cacheKey, localCache[cacheKey]); | |
| } | |
| } | |
| return cacheItems; | |
| } | |
| private Dictionary<string, LocalCacheObject> GetLocalCache() { | |
| var cache = new Dictionary<string, LocalCacheObject>(); | |
| if (HttpRuntime.Cache[cacheStore] != null) { | |
| cache = HttpRuntime.Cache[cacheStore] as Dictionary<string, LocalCacheObject>; | |
| var keys = cache.Keys.ToArray(); | |
| var now = DateTime.Now; | |
| foreach (var cacheKey in keys) { | |
| if (((LocalCacheObject)cache[cacheKey]).Expires < now) { | |
| cache.Remove(cacheKey); | |
| } | |
| } | |
| } | |
| HttpRuntime.Cache[cacheStore] = cache; | |
| return cache; | |
| } | |
| protected class LocalCacheObject { | |
| public DateTime Expires { get; set; } | |
| public object Value { get; set; } | |
| public LocalCacheObject(object value, TimeSpan expiry) { | |
| this.Value = value; | |
| this.Expires = DateTime.Now.Add(expiry); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment