From 8fea9530b5cb8bec7c16412733afd621d63698c4 Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 8 Aug 2013 20:25:26 +1000 Subject: [PATCH] Simplified the base cache provider, no generics, these will be taken care of by extensions --- .../Cache/CacheProviderExtensions.cs | 48 +++++++++++++++++++ .../Cache/DictionaryCacheProdiverBase.cs | 41 +++------------- .../Cache/HttpRequestCacheProvider.cs | 9 ++-- .../Cache/HttpRuntimeCacheProvider.cs | 3 +- src/Umbraco.Core/Cache/ICacheProvider.cs | 7 ++- src/Umbraco.Core/Cache/NullCacheProvider.cs | 14 ++---- .../Cache/ObjectCacheRuntimeCacheProvider.cs | 29 ++--------- src/Umbraco.Core/Cache/StaticCacheProvider.cs | 34 +++---------- .../Caching/IRepositoryCacheProvider.cs | 41 ++++++++++++++++ src/Umbraco.Core/Umbraco.Core.csproj | 1 + 10 files changed, 120 insertions(+), 107 deletions(-) create mode 100644 src/Umbraco.Core/Cache/CacheProviderExtensions.cs diff --git a/src/Umbraco.Core/Cache/CacheProviderExtensions.cs b/src/Umbraco.Core/Cache/CacheProviderExtensions.cs new file mode 100644 index 0000000000..475e4adfaf --- /dev/null +++ b/src/Umbraco.Core/Cache/CacheProviderExtensions.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Umbraco.Core.Cache +{ + /// + /// Extensions for strongly typed access + /// + internal static class CacheProviderExtensions + { + //T GetCacheItem(string cacheKey, TimeSpan? timeout, Func getCacheItem); + //T GetCacheItem(string cacheKey, CacheItemRemovedCallback refreshAction, TimeSpan? timeout, Func getCacheItem); + //T GetCacheItem(string cacheKey, CacheItemPriority priority, CacheItemRemovedCallback refreshAction, TimeSpan? timeout, Func getCacheItem); + //T GetCacheItem(string cacheKey, CacheItemPriority priority, CacheItemRemovedCallback refreshAction, CacheDependency cacheDependency, TimeSpan? timeout, Func getCacheItem); + + public static void ClearCacheObjectTypes(this ICacheProvider provider) + { + provider.ClearCacheObjectTypes(typeof(T).ToString()); + } + + public static IEnumerable GetCacheItemsByKeySearch(this ICacheProvider provider, string keyStartsWith) + { + var result = provider.GetCacheItemsByKeySearch(keyStartsWith); + return result.Select(x => ObjectExtensions.TryConvertTo(x).Result); + } + + public static T GetCacheItem(this ICacheProvider provider, string cacheKey) + { + var result = provider.GetCacheItem(cacheKey); + if (result == null) + { + return default(T); + } + return result.TryConvertTo().Result; + } + + public static T GetCacheItem(this ICacheProvider provider, string cacheKey, Func getCacheItem) + { + var result = provider.GetCacheItem(cacheKey, () => getCacheItem()); + if (result == null) + { + return default(T); + } + return result.TryConvertTo().Result; + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Cache/DictionaryCacheProdiverBase.cs b/src/Umbraco.Core/Cache/DictionaryCacheProdiverBase.cs index adc02f9104..b3955cc087 100644 --- a/src/Umbraco.Core/Cache/DictionaryCacheProdiverBase.cs +++ b/src/Umbraco.Core/Cache/DictionaryCacheProdiverBase.cs @@ -67,28 +67,7 @@ namespace Umbraco.Core.Cache } } } - - /// - /// Clears all objects in the System.Web.Cache with the System.Type specified - /// - public virtual void ClearCacheObjectTypes() - { - lock (Locker) - { - var keysToRemove = DictionaryCache.Cast() - .Select(item => new DictionaryItemWrapper(item)) - .Where(c => DictionaryCache[c.Key.ToString()] != null && DictionaryCache[c.Key.ToString()].GetType() == typeof (T)) - .Select(c => c.Key) - .ToList(); - - foreach (var k in keysToRemove) - { - DictionaryCache.Remove(k); - } - - } - } - + /// /// Clears all cache items that starts with the key passed. /// @@ -134,35 +113,27 @@ namespace Umbraco.Core.Cache } } - public virtual IEnumerable GetCacheItemsByKeySearch(string keyStartsWith) + public virtual IEnumerable GetCacheItemsByKeySearch(string keyStartsWith) { return (from object item in DictionaryCache select new DictionaryItemWrapper(item) into c where c.Key is string && ((string) c.Key).InvariantStartsWith(string.Format("{0}-{1}", CacheItemPrefix, keyStartsWith)) - select c.Value.TryConvertTo() - into converted - where converted.Success - select converted.Result).ToList(); + select c.Value).ToList(); } /// /// Returns a cache item by key, does not update the cache if it isn't there. /// - /// /// /// - public virtual TT GetCacheItem(string cacheKey) + public virtual object GetCacheItem(string cacheKey) { var result = DictionaryCache.Get(GetCacheKey(cacheKey)); - if (result == null) - { - return default(TT); - } - return result.TryConvertTo().Result; + return result; } - public abstract T GetCacheItem(string cacheKey, Func getCacheItem); + public abstract object GetCacheItem(string cacheKey, Func getCacheItem); /// /// We prefix all cache keys with this so that we know which ones this class has created when diff --git a/src/Umbraco.Core/Cache/HttpRequestCacheProvider.cs b/src/Umbraco.Core/Cache/HttpRequestCacheProvider.cs index 134b780f2b..26c6dc0af8 100644 --- a/src/Umbraco.Core/Cache/HttpRequestCacheProvider.cs +++ b/src/Umbraco.Core/Cache/HttpRequestCacheProvider.cs @@ -36,15 +36,12 @@ namespace Umbraco.Core.Cache } } - public override T GetCacheItem(string cacheKey, Func getCacheItem) + public override object GetCacheItem(string cacheKey, Func getCacheItem) { var ctx = _context(); var ck = GetCacheKey(cacheKey); - if (ctx.Items[ck] == null) - { - ctx.Items[ck] = getCacheItem(); - } - return (T)ctx.Items[ck]; + return ctx.Items[ck] ?? (ctx.Items[ck] = getCacheItem()); } + } } \ No newline at end of file diff --git a/src/Umbraco.Core/Cache/HttpRuntimeCacheProvider.cs b/src/Umbraco.Core/Cache/HttpRuntimeCacheProvider.cs index df4c545f3c..17cabbe903 100644 --- a/src/Umbraco.Core/Cache/HttpRuntimeCacheProvider.cs +++ b/src/Umbraco.Core/Cache/HttpRuntimeCacheProvider.cs @@ -29,11 +29,10 @@ namespace Umbraco.Core.Cache /// /// Gets (and adds if necessary) an item from the cache with all of the default parameters /// - /// /// /// /// - public override T GetCacheItem(string cacheKey, Func getCacheItem) + public override object GetCacheItem(string cacheKey, Func getCacheItem) { return GetCacheItem(cacheKey, CacheItemPriority.Normal, null, null, null, getCacheItem, Locker); } diff --git a/src/Umbraco.Core/Cache/ICacheProvider.cs b/src/Umbraco.Core/Cache/ICacheProvider.cs index ab70cf63c0..4c14083e1d 100644 --- a/src/Umbraco.Core/Cache/ICacheProvider.cs +++ b/src/Umbraco.Core/Cache/ICacheProvider.cs @@ -14,11 +14,10 @@ namespace Umbraco.Core.Cache void ClearAllCache(); void ClearCacheItem(string key); void ClearCacheObjectTypes(string typeName); - void ClearCacheObjectTypes(); void ClearCacheByKeySearch(string keyStartsWith); void ClearCacheByKeyExpression(string regexString); - IEnumerable GetCacheItemsByKeySearch(string keyStartsWith); - T GetCacheItem(string cacheKey); - T GetCacheItem(string cacheKey, Func getCacheItem); + IEnumerable GetCacheItemsByKeySearch(string keyStartsWith); + object GetCacheItem(string cacheKey); + object GetCacheItem(string cacheKey, Func getCacheItem); } } \ No newline at end of file diff --git a/src/Umbraco.Core/Cache/NullCacheProvider.cs b/src/Umbraco.Core/Cache/NullCacheProvider.cs index ebc59f2c25..6d1dc15e1f 100644 --- a/src/Umbraco.Core/Cache/NullCacheProvider.cs +++ b/src/Umbraco.Core/Cache/NullCacheProvider.cs @@ -19,10 +19,6 @@ namespace Umbraco.Core.Cache { } - public virtual void ClearCacheObjectTypes() - { - } - public virtual void ClearCacheByKeySearch(string keyStartsWith) { } @@ -31,17 +27,17 @@ namespace Umbraco.Core.Cache { } - public virtual IEnumerable GetCacheItemsByKeySearch(string keyStartsWith) + public virtual IEnumerable GetCacheItemsByKeySearch(string keyStartsWith) { - return Enumerable.Empty(); + return Enumerable.Empty(); } - public virtual T GetCacheItem(string cacheKey) + public virtual object GetCacheItem(string cacheKey) { - return default(T); + return default(object); } - public virtual T GetCacheItem(string cacheKey, Func getCacheItem) + public virtual object GetCacheItem(string cacheKey, Func getCacheItem) { return getCacheItem(); } diff --git a/src/Umbraco.Core/Cache/ObjectCacheRuntimeCacheProvider.cs b/src/Umbraco.Core/Cache/ObjectCacheRuntimeCacheProvider.cs index c9a37d830d..b237e0c8c8 100644 --- a/src/Umbraco.Core/Cache/ObjectCacheRuntimeCacheProvider.cs +++ b/src/Umbraco.Core/Cache/ObjectCacheRuntimeCacheProvider.cs @@ -54,18 +54,6 @@ namespace Umbraco.Core.Cache } } - public virtual void ClearCacheObjectTypes() - { - using (new WriteLock(ClearLock)) - { - var keysToRemove = (from c in MemoryCache where c.Value.GetType() == typeof (T) select c.Key).ToList(); - foreach (var k in keysToRemove) - { - MemoryCache.Remove(k); - } - } - } - public virtual void ClearCacheByKeySearch(string keyStartsWith) { using (new WriteLock(ClearLock)) @@ -90,27 +78,20 @@ namespace Umbraco.Core.Cache } } - public virtual IEnumerable GetCacheItemsByKeySearch(string keyStartsWith) + public virtual IEnumerable GetCacheItemsByKeySearch(string keyStartsWith) { return (from c in MemoryCache where c.Key.InvariantStartsWith(keyStartsWith) - select c.Value.TryConvertTo() - into attempt - where attempt.Success - select attempt.Result).ToList(); + select c.Value).ToList(); } - public virtual T GetCacheItem(string cacheKey) + public virtual object GetCacheItem(string cacheKey) { var result = MemoryCache.Get(cacheKey); - if (result == null) - { - return default(T); - } - return result.TryConvertTo().Result; + return result; } - public virtual T GetCacheItem(string cacheKey, Func getCacheItem) + public virtual object GetCacheItem(string cacheKey, Func getCacheItem) { return GetCacheItem(cacheKey, CacheItemPriority.Normal, null, null, null, getCacheItem); } diff --git a/src/Umbraco.Core/Cache/StaticCacheProvider.cs b/src/Umbraco.Core/Cache/StaticCacheProvider.cs index 67c0527393..c0b5e8ba5d 100644 --- a/src/Umbraco.Core/Cache/StaticCacheProvider.cs +++ b/src/Umbraco.Core/Cache/StaticCacheProvider.cs @@ -36,20 +36,7 @@ namespace Umbraco.Core.Cache StaticCache.TryRemove(key, out val); } } - } - - public virtual void ClearCacheObjectTypes() - { - foreach (var key in StaticCache.Keys) - { - if (StaticCache[key] != null - && StaticCache[key].GetType() == typeof(T)) - { - object val; - StaticCache.TryRemove(key, out val); - } - } - } + } public virtual void ClearCacheByKeySearch(string keyStartsWith) { @@ -73,29 +60,22 @@ namespace Umbraco.Core.Cache } } - public virtual IEnumerable GetCacheItemsByKeySearch(string keyStartsWith) + public virtual IEnumerable GetCacheItemsByKeySearch(string keyStartsWith) { return (from KeyValuePair c in StaticCache where c.Key.InvariantStartsWith(keyStartsWith) - select c.Value.TryConvertTo() - into attempt - where attempt.Success - select attempt.Result).ToList(); + select c.Value).ToList(); } - public virtual T GetCacheItem(string cacheKey) + public virtual object GetCacheItem(string cacheKey) { var result = StaticCache[cacheKey]; - if (result == null) - { - return default(T); - } - return result.TryConvertTo().Result; + return result; } - public virtual T GetCacheItem(string cacheKey, Func getCacheItem) + public virtual object GetCacheItem(string cacheKey, Func getCacheItem) { - return (T)StaticCache.GetOrAdd(cacheKey, getCacheItem()); + return StaticCache.GetOrAdd(cacheKey, getCacheItem()); } } diff --git a/src/Umbraco.Core/Persistence/Caching/IRepositoryCacheProvider.cs b/src/Umbraco.Core/Persistence/Caching/IRepositoryCacheProvider.cs index d03c81de1e..5f964b3b0f 100644 --- a/src/Umbraco.Core/Persistence/Caching/IRepositoryCacheProvider.cs +++ b/src/Umbraco.Core/Persistence/Caching/IRepositoryCacheProvider.cs @@ -1,9 +1,50 @@ using System; using System.Collections.Generic; +using Umbraco.Core.Cache; using Umbraco.Core.Models.EntityBase; namespace Umbraco.Core.Persistence.Caching { + //internal class RequestRepositoryCacheProvider : IRepositoryCacheProvider + //{ + // private readonly ICacheProvider _requestCacheProvider; + + // public RequestRepositoryCacheProvider(ICacheProvider requestCacheProvider) + // { + // _requestCacheProvider = requestCacheProvider; + // } + + // public IEntity GetById(Type type, Guid id) + // { + // throw new NotImplementedException(); + // } + + // public IEnumerable GetByIds(Type type, List ids) + // { + // throw new NotImplementedException(); + // } + + // public IEnumerable GetAllByType(Type type) + // { + // throw new NotImplementedException(); + // } + + // public void Save(Type type, IEntity entity) + // { + // throw new NotImplementedException(); + // } + + // public void Delete(Type type, IEntity entity) + // { + // throw new NotImplementedException(); + // } + + // public void Clear(Type type) + // { + // throw new NotImplementedException(); + // } + //} + /// /// Defines the implementation of a Cache Provider intented to back a repository /// diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index 75dcbf5a67..fd34a18159 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -122,6 +122,7 @@ +