- ICacheManager and ICache interfaces and default implementations for an Orchard cache.

- Unit tests.

--HG--
branch : dev
This commit is contained in:
Suha Can
2010-05-14 14:15:06 -07:00
parent a24b9eb732
commit d5cdc0d37e
8 changed files with 120 additions and 0 deletions

View File

@@ -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<DefaultCacheManager>().As<ICacheManager>();
_container = builder.Build();
_cacheManager = _container.Resolve<ICacheManager>();
}
[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"));
}
}
}

View File

@@ -117,6 +117,7 @@
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Caching\CacheTests.cs" />
<Compile Include="Commands\CommandHandlerDescriptorBuilderTests.cs" /> <Compile Include="Commands\CommandHandlerDescriptorBuilderTests.cs" />
<Compile Include="Commands\CommandHandlerTests.cs" /> <Compile Include="Commands\CommandHandlerTests.cs" />
<Compile Include="Commands\CommandManagerTests.cs" /> <Compile Include="Commands\CommandManagerTests.cs" />

View File

@@ -0,0 +1,4 @@
namespace Orchard.Caching {
public class AcquireContext {
}
}

View File

@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
namespace Orchard.Caching {
public class Cache<TKey, TResult> : ICache<TKey, TResult> {
private readonly Dictionary<TKey, CacheEntry> _entries;
public Cache() {
_entries = new Dictionary<TKey, CacheEntry>();
}
#region Implementation of ICache<TKey,TResult>
public TResult Get(TKey key, Func<AcquireContext, TResult> 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; }
}
}
}

View File

@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using Castle.Core;
namespace Orchard.Caching {
public class DefaultCacheManager : ICacheManager {
private readonly Dictionary<Pair<Type, Type>, object> _caches;
public DefaultCacheManager() {
_caches = new Dictionary<Pair<Type, Type>, object>();
}
#region Implementation of ICacheManager
public TResult Get<TKey, TResult>(TKey key, Func<AcquireContext, TResult> acquire) {
return GetCache<TKey, TResult>().Get(key, acquire);
}
public ICache<TKey, TResult> GetCache<TKey, TResult>() {
var cacheKey = new Pair<Type, Type>(typeof(TKey), typeof(TResult));
object value;
if (!_caches.TryGetValue(cacheKey, out value)) {
value = new Cache<TKey, TResult>();
_caches.Add(cacheKey, value);
}
return (ICache<TKey, TResult>)value;
}
#endregion
}
}

View File

@@ -0,0 +1,7 @@
using System;
namespace Orchard.Caching {
public interface ICache<TKey, TResult> {
TResult Get(TKey key, Func<AcquireContext, TResult> acquire);
}
}

View File

@@ -0,0 +1,8 @@
using System;
namespace Orchard.Caching {
public interface ICacheManager : ISingletonDependency {
TResult Get<TKey, TResult>(TKey key, Func<AcquireContext, TResult> acquire);
ICache<TKey, TResult> GetCache<TKey, TResult>();
}
}

View File

@@ -131,6 +131,11 @@
<Compile Include="Validation\Argument.cs" /> <Compile Include="Validation\Argument.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Caching\AcquireContext.cs" />
<Compile Include="Caching\Cache.cs" />
<Compile Include="Caching\DefaultCacheManager.cs" />
<Compile Include="Caching\ICache.cs" />
<Compile Include="Caching\ICacheManager.cs" />
<Compile Include="Commands\CommandParameters.cs" /> <Compile Include="Commands\CommandParameters.cs" />
<Compile Include="Commands\CommandDescriptor.cs" /> <Compile Include="Commands\CommandDescriptor.cs" />
<Compile Include="Commands\CommandHandlerDescriptor.cs" /> <Compile Include="Commands\CommandHandlerDescriptor.cs" />