Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Started to implement some of the concrete object caches

  • Loading branch information...
commit 915e24f9c1ba9e5f4ce4748d3f573decb6d470fc 1 parent d7389aa
@gravypower gravypower authored
Showing with 660 additions and 19 deletions.
  1. +42 −0 Source/Glass.Mapper.Sc/Caching/Configuration/SitecoreObjectCacheConfiguration.cs
  2. +26 −0 Source/Glass.Mapper.Sc/Caching/SitecoreCacheKey.cs
  3. +49 −0 Source/Glass.Mapper.Sc/Caching/SitecoreObjectCache.cs
  4. +3 −0  Source/Glass.Mapper.Sc/Glass.Mapper.Sc.csproj
  5. +1 −0  Source/Glass.Mapper/Caching/ObjectCaching/AbstractObjectCache.cs
  6. +41 −0 Source/Glass.Mapper/Caching/ObjectCaching/MemoryObjectCache.cs
  7. +106 −0 Tests/Unit Tests/Glass.Mapper.Sc.Tests/Caching/Configuration/SitecoreObjectCacheConfigurationFixture.cs
  8. +109 −0 Tests/Unit Tests/Glass.Mapper.Sc.Tests/Caching/ObjectCaching/SitecoreObjectCacheFixture.cs
  9. +48 −0 Tests/Unit Tests/Glass.Mapper.Sc.Tests/Caching/SitecoreCacheKeyFixture.cs
  10. +11 −0 Tests/Unit Tests/Glass.Mapper.Sc.Tests/Glass.Mapper.Sc.Tests.csproj
  11. +2 −0  Tests/Unit Tests/Glass.Mapper.Sc.Tests/packages.config
  12. +71 −6 Tests/Unit Tests/Glass.Mapper.Tests/Caching/Configuration/AbstractObjectCacheConfigurationFixture.cs
  13. +45 −7 Tests/Unit Tests/Glass.Mapper.Tests/Caching/Configuration/ContextFixture.cs
  14. +4 −6 Tests/Unit Tests/Glass.Mapper.Tests/Caching/ObjectCaching/AbstractObjectCacheFixture.cs
  15. +102 −0 Tests/Unit Tests/Glass.Mapper.Tests/Caching/ObjectCaching/MemoryObjectCacheFixture.cs
View
42 Source/Glass.Mapper.Sc/Caching/Configuration/SitecoreObjectCacheConfiguration.cs
@@ -0,0 +1,42 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Glass.Mapper.Caching.Configuration;
+using Glass.Mapper.Caching.ObjectCaching;
+
+namespace Glass.Mapper.Sc.Caching.Configuration
+{
+ /// <summary>
+ ///
+ /// </summary>
+ public class SitecoreObjectCacheConfiguration : AbstractObjectCacheConfiguration
+ {
+ /// <summary>
+ ///
+ /// </summary>
+ public SitecoreObjectCacheConfiguration()
+ : base(Context.Default.DependencyResolver)
+ {
+
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ /// <param name="contextName"></param>
+ public SitecoreObjectCacheConfiguration(string contextName)
+ : base(contextName)
+ {
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ /// <param name="resolver"></param>
+ public SitecoreObjectCacheConfiguration(IDependencyResolver resolver)
+ : base(resolver)
+ {
+ }
+ }
+}
View
26 Source/Glass.Mapper.Sc/Caching/SitecoreCacheKey.cs
@@ -0,0 +1,26 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Glass.Mapper.Caching;
+using Sitecore.Data;
+
+namespace Glass.Mapper.Sc.Caching
+{
+ /// <summary>
+ ///
+ /// </summary>
+ public class SitecoreCacheKey : CacheKey<ID>
+ {
+ /// <summary>
+ /// A cache key for the Sitecore CMS
+ /// </summary>
+ /// <param name="id">The ID of the Sitecore Node</param>
+ /// <param name="revisionId">The revision of the Sitecore Node</param>
+ /// <param name="database">The Database the node is from</param>
+ public SitecoreCacheKey(ID id, ID revisionId, string database):base(id, revisionId, database)
+ {
+
+ }
+ }
+}
View
49 Source/Glass.Mapper.Sc/Caching/SitecoreObjectCache.cs
@@ -0,0 +1,49 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.InteropServices;
+using System.Text;
+using Glass.Mapper.Caching.ObjectCaching;
+using Sitecore.Caching;
+using Sitecore.Data;
+
+namespace Glass.Mapper.Sc.Caching
+{
+ public class SitecoreObjectCache : AbstractObjectCache<ID>
+ {
+ private readonly Cache _objectCache;
+ private readonly TimeSpan _cacheItemSlidingExpiration;
+
+ public SitecoreObjectCache(AbstractCacheKeyResolver<ID> cacheKeyResolver)
+ : base(cacheKeyResolver)
+ {
+ _objectCache = Cache.GetNamedInstance("Glass.Mapper.Sc.Caching", GetCacheSize());
+
+ _cacheItemSlidingExpiration = System.Web.Caching.Cache.NoSlidingExpiration;
+ }
+
+ private static long GetCacheSize()
+ {
+ return Sitecore.StringUtil.ParseSizeString(
+ Sitecore.Configuration.Settings.GetSetting(
+ "Glass.Mapper.Sc.Caching.CacheSize", "100MB"
+ )
+ );
+ }
+
+ protected override bool InternalContansObject(string objectKey)
+ {
+ return _objectCache.ContainsKey(objectKey);
+ }
+
+ protected override void InternalAddObject(string objectKey, object objectForCaching)
+ {
+ _objectCache.Add(objectKey, objectForCaching, 10, _cacheItemSlidingExpiration);
+ }
+
+ protected override object InternalGetObject(string objectKey)
+ {
+ return _objectCache.GetValue(objectKey);
+ }
+ }
+}
View
3  Source/Glass.Mapper.Sc/Glass.Mapper.Sc.csproj
@@ -57,6 +57,9 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="Caching\Configuration\SitecoreObjectCacheConfiguration.cs" />
+ <Compile Include="Caching\SitecoreCacheKey.cs" />
+ <Compile Include="Caching\SitecoreObjectCache.cs" />
<Compile Include="CodeFirst\FieldInfo.cs" />
<Compile Include="CodeFirst\GlassDataProvider.cs" />
<Compile Include="CodeFirst\SectionInfo.cs" />
View
1  Source/Glass.Mapper/Caching/ObjectCaching/AbstractObjectCache.cs
@@ -47,5 +47,6 @@ protected AbstractObjectCache(AbstractCacheKeyResolver<TIdType> cacheKeyResolver
{
CacheKeyResolver = cacheKeyResolver;
}
+
}
}
View
41 Source/Glass.Mapper/Caching/ObjectCaching/MemoryObjectCache.cs
@@ -0,0 +1,41 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.Caching;
+using System.Text;
+
+namespace Glass.Mapper.Caching.ObjectCaching
+{
+ public class MemoryObjectCache<TIdType> : AbstractObjectCache<TIdType>
+ {
+ private volatile MemoryCache _objectCache;
+
+ /// <summary>
+ ///
+ /// </summary>
+ /// <param name="cacheKeyResolver"></param>
+ public MemoryObjectCache(AbstractCacheKeyResolver<TIdType> cacheKeyResolver)
+ : base(cacheKeyResolver)
+ {
+ _objectCache = new MemoryCache("Glass.Mapper.Caching.ObjectCaching");
+ }
+
+ protected override bool InternalContansObject(string objectKey)
+ {
+ return _objectCache.Contains(objectKey);
+ }
+
+ protected override void InternalAddObject(string objectKey, object objectForCaching)
+ {
+ var policy = new CacheItemPolicy();
+ policy.SlidingExpiration = new TimeSpan(0, 2, 0, 0);
+
+ _objectCache.Set(objectKey, objectForCaching, policy);
+ }
+
+ protected override object InternalGetObject(string objectKey)
+ {
+ return _objectCache.Get(objectKey);
+ }
+ }
+}
View
106 Tests/Unit Tests/Glass.Mapper.Sc.Tests/Caching/Configuration/SitecoreObjectCacheConfigurationFixture.cs
@@ -0,0 +1,106 @@
+using System.Collections;
+using Castle.MicroKernel.Registration;
+using Castle.Windsor;
+using Glass.Mapper.Caching.Configuration;
+using Glass.Mapper.Caching.ObjectCaching;
+using Glass.Mapper.Sc.Caching.Configuration;
+using NUnit.Framework;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Glass.Mapper.Sc.Tests.Caching.Configuration
+{
+ [TestFixture]
+ public class SitecoreObjectCacheConfigurationFixture
+ {
+ private StubIDependencyResolver stubIDependencyResolver;
+
+ [SetUp]
+ public void SetUp()
+ {
+ IWindsorContainer container = new WindsorContainer();
+ container.Register(
+ Component.For<AbstractObjectCacheConfiguration>()
+ .ImplementedBy<SitecoreObjectCacheConfiguration>()
+ .LifestyleTransient(),
+ Component.For<IAbstractObjectCache>()
+ .ImplementedBy<StubAbstractObjectCache>()
+ .LifestyleTransient()
+ );
+
+ stubIDependencyResolver = new StubIDependencyResolver(container);
+
+ Context.Create(stubIDependencyResolver);
+ }
+
+ [Test]
+ public void CanCreateAbstractObjectCacheConfiguration()
+ {
+ var objectCacheConfiguration = new SitecoreObjectCacheConfiguration();
+
+ Assert.IsNotNull(objectCacheConfiguration.ObjectCache);
+ }
+
+ [Test]
+ public void CanCreateAbstractObjectCacheConfigurationByName()
+ {
+ var objectCacheConfiguration = new SitecoreObjectCacheConfiguration("Default");
+
+ Assert.IsNotNull(objectCacheConfiguration.ObjectCache);
+ }
+
+ [Test]
+ public void CanCreateAbstractObjectCacheConfigurationByResolver()
+ {
+ var objectCacheConfiguration = new SitecoreObjectCacheConfiguration(stubIDependencyResolver);
+
+ Assert.IsNotNull(objectCacheConfiguration.ObjectCache);
+ }
+
+ }
+
+ public class StubAbstractObjectCache : IAbstractObjectCache
+ {
+
+ public bool ContansObject(Mapper.Pipelines.ObjectConstruction.ObjectCachingArgs args)
+ {
+ throw new NotImplementedException();
+ }
+
+ public object GetObject(Mapper.Pipelines.ObjectConstruction.ObjectCachingArgs args)
+ {
+ throw new NotImplementedException();
+ }
+
+ public void AddObject(Mapper.Pipelines.ObjectConstruction.ObjectCachingArgs args)
+ {
+ throw new NotImplementedException();
+ }
+ }
+
+ public class StubIDependencyResolver : IDependencyResolver
+ {
+ public IWindsorContainer Container { get; private set; }
+
+ public StubIDependencyResolver(IWindsorContainer container)
+ {
+ Container = container;
+ }
+
+ public T Resolve<T>(IDictionary<string, object> args = null)
+ {
+ if (args == null)
+ return Container.Resolve<T>();
+
+
+ return Container.Resolve<T>((IDictionary) args);
+ }
+
+ public IEnumerable<T> ResolveAll<T>()
+ {
+ return Container.ResolveAll<T>();
+ }
+ }
+}
View
109 Tests/Unit Tests/Glass.Mapper.Sc.Tests/Caching/ObjectCaching/SitecoreObjectCacheFixture.cs
@@ -0,0 +1,109 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Glass.Mapper.Caching;
+using Glass.Mapper.Caching.ObjectCaching;
+using Glass.Mapper.Pipelines.ObjectConstruction;
+using Glass.Mapper.Sc.Caching;
+using NSubstitute;
+using NUnit.Framework;
+using Sitecore.Data;
+using Glass.Mapper.Caching.Exceptions;
+
+namespace Glass.Mapper.Sc.Tests.Caching.ObjectCaching
+{
+ [TestFixture]
+ public class SitecoreObjectCacheFixture
+ {
+ private AbstractCacheKeyResolver<ID> _cacheKeyResolver;
+ private const string Database = "SomeDatabase";
+
+ [SetUp]
+ public void SetUp()
+ {
+ _cacheKeyResolver = Substitute.For<AbstractCacheKeyResolver<ID>>();
+ }
+
+ [Test]
+ public void AbstractObjectCacheContainObject()
+ {
+ var id = new ID(new Guid("e6c031ee-6ebd-46a3-a0cb-f79798f4fdd7"));
+ var revisionId = new ID(new Guid("e6c031ee-6ebd-46a3-a0cb-f79798f4fdd7"));
+ var args = Substitute.For<ObjectCachingArgs>();
+ var stubClass = new StubClass();
+ var key = Substitute.For<CacheKey<ID>>(id, revisionId, Database);
+
+ args.Result = stubClass;
+ args.CacheKey = key;
+
+ _cacheKeyResolver.GetKey(args).Returns(key);
+
+
+ //Assign
+ var abstractObjectCache = new SitecoreObjectCache(_cacheKeyResolver);
+
+ //Act
+ abstractObjectCache.AddObject(args);
+
+ //Assert
+ Assert.IsTrue(abstractObjectCache.ContansObject(args));
+ }
+
+ [Test]
+ public void AbstractObjectCacheAddObject()
+ {
+ //Assign
+
+ var id = new ID(new Guid("32e12d0b-db0c-4201-bcaf-302ec53c7309"));
+ var revisionId = new ID(new Guid("32e12d0b-db0c-4201-bcaf-302ec53c7309"));
+ var args = Substitute.For<ObjectCachingArgs>();
+ var stubClass = new StubClass();
+ var key = Substitute.For<CacheKey<ID>>(id, revisionId, Database);
+
+ args.Result = stubClass;
+ args.CacheKey = key;
+
+ _cacheKeyResolver.GetKey(args).Returns(key);
+
+ var abstractObjectCache = new SitecoreObjectCache(_cacheKeyResolver);
+ abstractObjectCache.AddObject(args);
+ _cacheKeyResolver.GetKey(args).Returns(key);
+ args.CacheKey = key;
+
+ //Act
+ abstractObjectCache.GetObject(args).Returns(key);
+
+ //Assert
+ Assert.AreSame(stubClass, abstractObjectCache.GetObject(args));
+ }
+
+ [Test]
+ [ExpectedException(typeof(DuplicatedKeyObjectCacheException))]
+ public void ExpectWhenTryToAddObjectAgain()
+ {
+ //Assign
+ var abstractObjectCache = new SitecoreObjectCache(_cacheKeyResolver);
+
+ var id = new ID(new Guid("d1f6f2b8-93dd-477d-8089-c3639e713fab"));
+ var revisionId = new ID(new Guid("d1f6f2b8-93dd-477d-8089-c3639e713fab"));
+ var args = Substitute.For<ObjectCachingArgs>();
+ var stubClass = new StubClass();
+ var key = Substitute.For<CacheKey<ID>>(id, revisionId, Database);
+
+ _cacheKeyResolver.GetKey(args).Returns(key);
+
+ args.Result = stubClass;
+ args.CacheKey = key;
+
+ //Act
+ abstractObjectCache.AddObject(args);
+ abstractObjectCache.AddObject(args);
+ }
+ }
+
+ public class StubClass
+ {
+
+ }
+}
View
48 Tests/Unit Tests/Glass.Mapper.Sc.Tests/Caching/SitecoreCacheKeyFixture.cs
@@ -0,0 +1,48 @@
+using Glass.Mapper.Sc.Caching;
+using NUnit.Framework;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Sitecore.Data;
+
+namespace Glass.Mapper.Sc.Tests.Caching
+{
+ [TestFixture]
+ public class SitecoreCacheKeyFixture
+ {
+ private ID _id = null;
+ private ID _revisionId = null;
+ private const string Database = "SomeDatabase";
+ private SitecoreCacheKey _key;
+
+ [SetUp]
+ public void Setup()
+ {
+ _id = new ID(new Guid());
+ _revisionId = new ID(new Guid());
+ _key = new SitecoreCacheKey(_id, _revisionId, Database);
+ }
+
+ [Test]
+ public void CanCreateSitecoreCacheKey()
+ {
+ //Assert
+ Assert.AreEqual(_key.Database, Database);
+ Assert.AreEqual(_key.Id, _id);
+ Assert.AreEqual(_key.GetId(), _id);
+ Assert.AreEqual(_key.RevisionId, _revisionId);
+ Assert.AreEqual(_key.KeyType, typeof(ID));
+ }
+
+ [Test]
+ public void CanGetSitecoreCacheKeyString()
+ {
+ //Assign
+ var keyStrng = "{0},{1},{2},{3}".Formatted(_id, _revisionId, Database, typeof(ID));
+
+ //Assert
+ Assert.AreEqual(keyStrng, _key.ToString());
+ }
+ }
+}
View
11 Tests/Unit Tests/Glass.Mapper.Sc.Tests/Glass.Mapper.Sc.Tests.csproj
@@ -42,6 +42,13 @@
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
+ <Reference Include="Castle.Core, Version=3.2.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\..\..\packages\Castle.Core.3.2.0\lib\net40-client\Castle.Core.dll</HintPath>
+ </Reference>
+ <Reference Include="Castle.Windsor">
+ <HintPath>..\..\..\packages\Castle.Windsor.3.2.0\lib\net40\Castle.Windsor.dll</HintPath>
+ </Reference>
<Reference Include="NSubstitute, Version=1.5.0.0, Culture=neutral, PublicKeyToken=92dd2e9066daa5ca, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\packages\NSubstitute.1.5.0.0\lib\NET40\NSubstitute.dll</HintPath>
@@ -62,6 +69,9 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="Caching\Configuration\SitecoreObjectCacheConfigurationFixture.cs" />
+ <Compile Include="Caching\ObjectCaching\SitecoreObjectCacheFixture.cs" />
+ <Compile Include="Caching\SitecoreCacheKeyFixture.cs" />
<Compile Include="Configuration\Attributes\SitecoreAttributeConfigurationLoaderFixture.cs" />
<Compile Include="Configuration\Attributes\SitecoreChildrenAttributeFixture.cs" />
<Compile Include="Configuration\Attributes\SitecoreQueryAttributeFixture.cs" />
@@ -88,6 +98,7 @@
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
+ <ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
View
2  Tests/Unit Tests/Glass.Mapper.Sc.Tests/packages.config
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
+ <package id="Castle.Core" version="3.2.0" targetFramework="net40" />
+ <package id="Castle.Windsor" version="3.2.0" targetFramework="net40" />
<package id="NSubstitute" version="1.5.0.0" targetFramework="net40" />
<package id="NUnit" version="2.6.2" targetFramework="net40" />
</packages>
View
77 Tests/Unit Tests/Glass.Mapper.Tests/Caching/Configuration/AbstractObjectCacheConfigurationFixture.cs
@@ -6,6 +6,7 @@
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Glass.Mapper.Caching.Configuration;
+using Glass.Mapper.Caching.ObjectCaching;
using NUnit.Framework;
namespace Glass.Mapper.Tests.Caching.Configuration
@@ -14,7 +15,7 @@ namespace Glass.Mapper.Tests.Caching.Configuration
public class AbstractObjectCacheConfigurationFixture
{
private StubIDependencyResolver stubIDependencyResolver;
-
+
[SetUp]
public void SetUp()
{
@@ -22,17 +23,81 @@ public void SetUp()
container.Register(
Component.For<AbstractObjectCacheConfiguration>()
.ImplementedBy<StubAbstractObjectCacheConfiguration>()
- .LifestyleTransient());
+ .LifestyleTransient(),
+ Component.For<IAbstractObjectCache>()
+ .ImplementedBy<StubAbstractObjectCache>()
+ .LifestyleTransient()
+
+ );
stubIDependencyResolver = new StubIDependencyResolver(container);
+
+ Context.Create(stubIDependencyResolver);
+ }
+
+ [Test]
+ public void CanCreateAbstractObjectCacheConfiguration()
+ {
+ var objectCacheConfiguration = new StubAbstractObjectCacheConfiguration();
+
+ Assert.IsNotNull(objectCacheConfiguration.ObjectCache);
+ }
+
+ [Test]
+ public void CanCreateAbstractObjectCacheConfigurationByName()
+ {
+ var objectCacheConfiguration = new StubAbstractObjectCacheConfiguration("Default");
+
+ Assert.IsNotNull(objectCacheConfiguration.ObjectCache);
+ }
+
+ [Test]
+ public void CanCreateAbstractObjectCacheConfigurationByResolver()
+ {
+ var objectCacheConfiguration = new StubAbstractObjectCacheConfiguration(stubIDependencyResolver);
+
+ Assert.IsNotNull(objectCacheConfiguration.ObjectCache);
}
}
- public class StubAbstractObjectCacheConfiguration:AbstractObjectCacheConfiguration
+ public class StubAbstractObjectCacheConfiguration : AbstractObjectCacheConfiguration
{
-
+ public StubAbstractObjectCacheConfiguration()
+ : base()
+ {
+ }
+
+ public StubAbstractObjectCacheConfiguration(string contextName)
+ : base(contextName)
+ {
+ }
+
+ public StubAbstractObjectCacheConfiguration(IDependencyResolver resolver)
+ : base(resolver)
+ {
+ }
}
- public class StubIDependencyResolver:IDependencyResolver
+
+ public class StubAbstractObjectCache : IAbstractObjectCache
+ {
+
+ public bool ContansObject(Mapper.Pipelines.ObjectConstruction.ObjectCachingArgs args)
+ {
+ throw new NotImplementedException();
+ }
+
+ public object GetObject(Mapper.Pipelines.ObjectConstruction.ObjectCachingArgs args)
+ {
+ throw new NotImplementedException();
+ }
+
+ public void AddObject(Mapper.Pipelines.ObjectConstruction.ObjectCachingArgs args)
+ {
+ throw new NotImplementedException();
+ }
+ }
+
+ public class StubIDependencyResolver : IDependencyResolver
{
public IWindsorContainer Container { get; private set; }
@@ -47,7 +112,7 @@ public T Resolve<T>(IDictionary<string, object> args = null)
return Container.Resolve<T>();
- return Container.Resolve<T>((IDictionary)args);
+ return Container.Resolve<T>((IDictionary) args);
}
public IEnumerable<T> ResolveAll<T>()
View
52 Tests/Unit Tests/Glass.Mapper.Tests/Caching/Configuration/ContextFixture.cs
@@ -15,18 +15,56 @@ namespace Glass.Mapper.Tests.Caching.Configuration
[TestFixture]
public class ContextFixture
{
+ private ContextFixtureIDependencyResolver _contextFixtureIDependencyResolver;
+
+ [SetUp]
+ public void SetUp()
+ {
+ _contextFixtureIDependencyResolver = new ContextFixtureIDependencyResolver();
+ }
+
[Test]
public void CanConfigureCache()
- {
- //create the resolver
- var resolver = new ContextFixtureIDependencyResolver();
-
+ {
//create a context
- var context = Context.Create(resolver);
+ var context = Context.Create(_contextFixtureIDependencyResolver);
context.ConfigureCache();
Assert.IsNotNull(context.ObjectCacheConfiguration);
}
+
+ [Test]
+ public void CanConfigureCacheManuley()
+ {
+ //create a context
+ var context = Context.Create(_contextFixtureIDependencyResolver);
+ context.ConfigureCache(new ContextFixtureAbstractObjectCacheConfiguration());
+
+ Assert.IsNotNull(context.ObjectCacheConfiguration);
+ }
+ }
+ public class ContextFixtureIAbstractObjectCache:IAbstractObjectCache
+ {
+
+ public bool ContansObject(Mapper.Pipelines.ObjectConstruction.ObjectCachingArgs args)
+ {
+ throw new NotImplementedException();
+ }
+
+ public object GetObject(Mapper.Pipelines.ObjectConstruction.ObjectCachingArgs args)
+ {
+ throw new NotImplementedException();
+ }
+
+ public void AddObject(Mapper.Pipelines.ObjectConstruction.ObjectCachingArgs args)
+ {
+ throw new NotImplementedException();
+ }
+ }
+
+ public class ContextFixtureAbstractObjectCacheConfiguration:AbstractObjectCacheConfiguration
+ {
+
}
public class ContextFixtureIDependencyResolver : IDependencyResolver
@@ -37,8 +75,8 @@ public ContextFixtureIDependencyResolver()
{
container = new WindsorContainer();
container.Register(
- Component.For<IAbstractObjectCache>().ImplementedBy<IAbstractObjectCache>().LifestyleTransient(),
- Component.For<AbstractObjectCacheConfiguration>().ImplementedBy<AbstractObjectCacheConfiguration>().LifestyleTransient());
+ Component.For<IAbstractObjectCache>().ImplementedBy<ContextFixtureIAbstractObjectCache>().LifestyleTransient(),
+ Component.For<AbstractObjectCacheConfiguration>().ImplementedBy<ContextFixtureAbstractObjectCacheConfiguration>().LifestyleTransient());
}
/// <summary>
View
10 Tests/Unit Tests/Glass.Mapper.Tests/Caching/ObjectCaching/AbstractObjectCacheFixture.cs
@@ -1,8 +1,4 @@
-using System;
-using System.Collections;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
+using System.Collections;
using Glass.Mapper.Caching;
using Glass.Mapper.Caching.Exceptions;
using Glass.Mapper.Caching.ObjectCaching;
@@ -32,11 +28,12 @@ public void SetUp()
_args.Result = _stubObject;
_cacheKeyResolver = Substitute.For<AbstractCacheKeyResolver<int>>();
- _key = Substitute.For<CacheKey<int>>(42, RevisionId, Database);
+ _key = Substitute.For<CacheKey<int>>(Id, RevisionId, Database);
_args.CacheKey = _key;
_cacheKeyResolver.GetKey(_args).Returns(_key);
}
+
[Test]
public void AbstractObjectCacheContainObject()
{
@@ -89,6 +86,7 @@ public StubAbstractObjectCache(AbstractCacheKeyResolver<int> cacheKeyResolver)
: base(cacheKeyResolver)
{
}
+
protected override bool InternalContansObject(string objectKey)
{
return _table.ContainsKey(objectKey);
View
102 Tests/Unit Tests/Glass.Mapper.Tests/Caching/ObjectCaching/MemoryObjectCacheFixture.cs
@@ -0,0 +1,102 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Glass.Mapper.Caching;
+using Glass.Mapper.Caching.Exceptions;
+using Glass.Mapper.Caching.ObjectCaching;
+using Glass.Mapper.Pipelines.ObjectConstruction;
+using NSubstitute;
+using NUnit.Framework;
+
+namespace Glass.Mapper.Tests.Caching.ObjectCaching
+{
+ [TestFixture]
+ public class MemoryObjectCacheFixture
+ {
+ private AbstractCacheKeyResolver<int> _cacheKeyResolver;
+ private const string Database = "SomeDatabase";
+
+ [SetUp]
+ public void SetUp()
+ {
+ _cacheKeyResolver = Substitute.For<AbstractCacheKeyResolver<int>>();
+ }
+
+ [Test]
+ public void AbstractObjectCacheContainObject()
+ {
+ var id = 1;
+ var revisionId = 1;
+ var args = Substitute.For<ObjectCachingArgs>();
+ var stubClass = new StubClass();
+ var key = Substitute.For<CacheKey<int>>(id, revisionId, Database);
+
+ args.Result = stubClass;
+ args.CacheKey = key;
+
+ _cacheKeyResolver.GetKey(args).Returns(key);
+
+
+ //Assign
+ var abstractObjectCache = new MemoryObjectCache<int>(_cacheKeyResolver);
+
+ //Act
+ abstractObjectCache.AddObject(args);
+
+ //Assert
+ Assert.IsTrue(abstractObjectCache.ContansObject(args));
+ }
+
+ [Test]
+ public void AbstractObjectCacheAddObject()
+ {
+ //Assign
+
+ var id = 2;
+ var revisionId = 2;
+ var args = Substitute.For<ObjectCachingArgs>();
+ var stubClass = new StubClass();
+ var key = Substitute.For<CacheKey<int>>(id, revisionId, Database);
+
+ args.Result = stubClass;
+ args.CacheKey = key;
+
+ _cacheKeyResolver.GetKey(args).Returns(key);
+
+ var abstractObjectCache = new MemoryObjectCache<int>(_cacheKeyResolver);
+ abstractObjectCache.AddObject(args);
+ _cacheKeyResolver.GetKey(args).Returns(key);
+ args.CacheKey = key;
+
+ //Act
+ abstractObjectCache.GetObject(args).Returns(key);
+
+ //Assert
+ Assert.AreSame(stubClass, abstractObjectCache.GetObject(args));
+ }
+
+ [Test]
+ [ExpectedException(typeof(DuplicatedKeyObjectCacheException))]
+ public void ExpectWhenTryToAddObjectAgain()
+ {
+ //Assign
+ var abstractObjectCache = new MemoryObjectCache<int>(_cacheKeyResolver);
+
+ var id = 3;
+ var revisionId = 3;
+ var args = Substitute.For<ObjectCachingArgs>();
+ var stubClass = new StubClass();
+ var key = Substitute.For<CacheKey<int>>(id, revisionId, Database);
+
+ _cacheKeyResolver.GetKey(args).Returns(key);
+
+ args.Result = stubClass;
+ args.CacheKey = key;
+
+ //Act
+ abstractObjectCache.AddObject(args);
+ abstractObjectCache.AddObject(args);
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.