mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
- ICacheManager and ICache interfaces and default implementations for an Orchard cache.
- Unit tests. --HG-- branch : dev
This commit is contained in:
32
src/Orchard.Tests/Caching/CacheTests.cs
Normal file
32
src/Orchard.Tests/Caching/CacheTests.cs
Normal 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"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -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" />
|
||||||
|
4
src/Orchard/Caching/AcquireContext.cs
Normal file
4
src/Orchard/Caching/AcquireContext.cs
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
namespace Orchard.Caching {
|
||||||
|
public class AcquireContext {
|
||||||
|
}
|
||||||
|
}
|
31
src/Orchard/Caching/Cache.cs
Normal file
31
src/Orchard/Caching/Cache.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
32
src/Orchard/Caching/DefaultCacheManager.cs
Normal file
32
src/Orchard/Caching/DefaultCacheManager.cs
Normal 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
|
||||||
|
}
|
||||||
|
}
|
7
src/Orchard/Caching/ICache.cs
Normal file
7
src/Orchard/Caching/ICache.cs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Orchard.Caching {
|
||||||
|
public interface ICache<TKey, TResult> {
|
||||||
|
TResult Get(TKey key, Func<AcquireContext, TResult> acquire);
|
||||||
|
}
|
||||||
|
}
|
8
src/Orchard/Caching/ICacheManager.cs
Normal file
8
src/Orchard/Caching/ICacheManager.cs
Normal 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>();
|
||||||
|
}
|
||||||
|
}
|
@@ -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" />
|
||||||
|
Reference in New Issue
Block a user