From d5cdc0d37e64eee033764f497752dded8f6fbe99 Mon Sep 17 00:00:00 2001 From: Suha Can Date: Fri, 14 May 2010 14:15:06 -0700 Subject: [PATCH] - ICacheManager and ICache interfaces and default implementations for an Orchard cache. - Unit tests. --HG-- branch : dev --- src/Orchard.Tests/Caching/CacheTests.cs | 32 +++++++++++++++++++ .../Orchard.Framework.Tests.csproj | 1 + src/Orchard/Caching/AcquireContext.cs | 4 +++ src/Orchard/Caching/Cache.cs | 31 ++++++++++++++++++ src/Orchard/Caching/DefaultCacheManager.cs | 32 +++++++++++++++++++ src/Orchard/Caching/ICache.cs | 7 ++++ src/Orchard/Caching/ICacheManager.cs | 8 +++++ src/Orchard/Orchard.Framework.csproj | 5 +++ 8 files changed, 120 insertions(+) create mode 100644 src/Orchard.Tests/Caching/CacheTests.cs create mode 100644 src/Orchard/Caching/AcquireContext.cs create mode 100644 src/Orchard/Caching/Cache.cs create mode 100644 src/Orchard/Caching/DefaultCacheManager.cs create mode 100644 src/Orchard/Caching/ICache.cs create mode 100644 src/Orchard/Caching/ICacheManager.cs diff --git a/src/Orchard.Tests/Caching/CacheTests.cs b/src/Orchard.Tests/Caching/CacheTests.cs new file mode 100644 index 000000000..12e91490d --- /dev/null +++ b/src/Orchard.Tests/Caching/CacheTests.cs @@ -0,0 +1,32 @@ +using Autofac; +using NUnit.Framework; +using Orchard.Caching; + +namespace Orchard.Tests.Caching { + [TestFixture] + public class CacheTests { + private IContainer _container; + private ICacheManager _cacheManager; + + [SetUp] + public void Init() { + var builder = new ContainerBuilder(); + builder.RegisterType().As(); + _container = builder.Build(); + _cacheManager = _container.Resolve(); + } + + [Test] + public void CacheManagerShouldReturnCacheItem() { + var result = _cacheManager.Get("testItem", ctx => "testResult"); + Assert.That(result, Is.EqualTo("testResult")); + } + + [Test] + public void CacheManagerShouldReturnExistingCacheItem() { + _cacheManager.Get("testItem", ctx => "testResult"); + var result = _cacheManager.Get("testItem", ctx => ""); + Assert.That(result, Is.EqualTo("testResult")); + } + } +} diff --git a/src/Orchard.Tests/Orchard.Framework.Tests.csproj b/src/Orchard.Tests/Orchard.Framework.Tests.csproj index 245f6cfd3..7eb159c9c 100644 --- a/src/Orchard.Tests/Orchard.Framework.Tests.csproj +++ b/src/Orchard.Tests/Orchard.Framework.Tests.csproj @@ -117,6 +117,7 @@ + diff --git a/src/Orchard/Caching/AcquireContext.cs b/src/Orchard/Caching/AcquireContext.cs new file mode 100644 index 000000000..8af2a750c --- /dev/null +++ b/src/Orchard/Caching/AcquireContext.cs @@ -0,0 +1,4 @@ +namespace Orchard.Caching { + public class AcquireContext { + } +} diff --git a/src/Orchard/Caching/Cache.cs b/src/Orchard/Caching/Cache.cs new file mode 100644 index 000000000..4f8dc73ab --- /dev/null +++ b/src/Orchard/Caching/Cache.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; + +namespace Orchard.Caching { + public class Cache : ICache { + private readonly Dictionary _entries; + + public Cache() { + _entries = new Dictionary(); + } + + #region Implementation of ICache + + public TResult Get(TKey key, Func acquire) { + CacheEntry entry; + if (!_entries.TryGetValue(key, out entry)) { + AcquireContext context = new AcquireContext(); + entry = new CacheEntry {Result = acquire(context)}; + _entries.Add(key, entry); + } + return entry.Result; + } + + #endregion + + public class CacheEntry { + public TResult Result { get; set; } + } + } + +} diff --git a/src/Orchard/Caching/DefaultCacheManager.cs b/src/Orchard/Caching/DefaultCacheManager.cs new file mode 100644 index 000000000..1535bf852 --- /dev/null +++ b/src/Orchard/Caching/DefaultCacheManager.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using Castle.Core; + +namespace Orchard.Caching { + public class DefaultCacheManager : ICacheManager { + private readonly Dictionary, object> _caches; + + public DefaultCacheManager() { + _caches = new Dictionary, object>(); + } + + #region Implementation of ICacheManager + + public TResult Get(TKey key, Func acquire) { + return GetCache().Get(key, acquire); + } + + public ICache GetCache() { + var cacheKey = new Pair(typeof(TKey), typeof(TResult)); + object value; + if (!_caches.TryGetValue(cacheKey, out value)) { + value = new Cache(); + _caches.Add(cacheKey, value); + } + + return (ICache)value; + } + + #endregion + } +} diff --git a/src/Orchard/Caching/ICache.cs b/src/Orchard/Caching/ICache.cs new file mode 100644 index 000000000..98eff848c --- /dev/null +++ b/src/Orchard/Caching/ICache.cs @@ -0,0 +1,7 @@ +using System; + +namespace Orchard.Caching { + public interface ICache { + TResult Get(TKey key, Func acquire); + } +} diff --git a/src/Orchard/Caching/ICacheManager.cs b/src/Orchard/Caching/ICacheManager.cs new file mode 100644 index 000000000..29d643d9e --- /dev/null +++ b/src/Orchard/Caching/ICacheManager.cs @@ -0,0 +1,8 @@ +using System; + +namespace Orchard.Caching { + public interface ICacheManager : ISingletonDependency { + TResult Get(TKey key, Func acquire); + ICache GetCache(); + } +} diff --git a/src/Orchard/Orchard.Framework.csproj b/src/Orchard/Orchard.Framework.csproj index 06774acad..f5378536f 100644 --- a/src/Orchard/Orchard.Framework.csproj +++ b/src/Orchard/Orchard.Framework.csproj @@ -131,6 +131,11 @@ + + + + +