Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

moved the object caching to the glass context made no sense to have i…

…t in the construction args
  • Loading branch information...
commit e452928483f0eb1f4c27407f3a3b26201bcc49a8 1 parent ff027a3
@gravypower gravypower authored
Showing with 1,028 additions and 122 deletions.
  1. +0 −5 Sites/Sitecore/Glass.Mapper.Sites.Sc/Glass.Mapper.Sites.Sc.ncrunchproject
  2. +1 −1  Source/Glass.Mapper.Sc/Caching/CacheKeyResolving/Implementations/SitecoreCacheKey.cs
  3. +1 −4 Source/Glass.Mapper.Sc/GlassConfig.cs
  4. +2 −17 Source/Glass.Mapper/AbstractService.cs
  5. +1 −4 Source/Glass.Mapper/Caching/CacheKeyResolving/CacheKey.cs
  6. +64 −0 Source/Glass.Mapper/Caching/ObjectCaching/AbstractObjectCache.cs
  7. +3 −1 Source/Glass.Mapper/Caching/ObjectCaching/IAbstractObjectCache.cs
  8. +14 −9 Source/Glass.Mapper/Caching/ObjectCaching/Implementations/CacheTable.cs
  9. +56 −0 Source/Glass.Mapper/Caching/ObjectCaching/Implementations/MemoryCache.cs
  10. +0 −31 Source/Glass.Mapper/Caching/ObjectCaching/ObjectCache.cs
  11. +8 −1 Source/Glass.Mapper/Caching/Proxy/CacheMethodInterceptor.cs
  12. +13 −8 Source/Glass.Mapper/CastleWindsor/CastleDependencyResolver.cs
  13. +1 −1  Source/Glass.Mapper/Configuration/AbstractObjectCacheConfiguration.cs
  14. +5 −4 Source/Glass.Mapper/Context.cs
  15. +3 −1 Source/Glass.Mapper/Glass.Mapper.csproj
  16. +1 −0  Source/Glass.Mapper/IDependencyResolver.cs
  17. +1 −12 Source/Glass.Mapper/Pipelines/ObjectConstruction/ObjectConstructionArgs.cs
  18. +1 −1  Source/Glass.Mapper/Pipelines/ObjectConstruction/Tasks/CreateConcrete/CreateConcreteTask.cs
  19. +3 −6 Source/Glass.Mapper/Pipelines/ObjectConstruction/Tasks/ObjectCachingResolver/ObjectCachingResolverTask.cs
  20. +1 −1  Source/Glass.Mapper/Pipelines/ObjectConstruction/Tasks/ObjectCachingSaver/ObjectCachingSaverTask.cs
  21. +4 −4 Tests/Integration Tests/Sitecore/Glass.Mapper.Sc.Integration/SitecoreServiceFixture.cs
  22. +1 −1  Tests/Unit Tests/Glass.Mapper.Sc.Tests/Caching/SitecoreCacheKeyResolverFixture.cs
  23. +828 −0 Tests/Unit Tests/Glass.Mapper.Tests/Caching/AbstractObjectCacheFixture.cs
  24. +4 −2 Tests/Unit Tests/Glass.Mapper.Tests/Caching/CacheKeyResolverFixture.cs
  25. +2 −5 Tests/Unit Tests/Glass.Mapper.Tests/Caching/CacheTableFixture.cs
  26. +1 −0  Tests/Unit Tests/Glass.Mapper.Tests/Glass.Mapper.Tests.csproj
  27. +6 −2 ...s.Mapper.Tests/Pipelines/ObjectConstruction/Tasks/ObjectCachingResolver/ObjectCachingResolverTaskFixture.cs
  28. +3 −1 ...s/Glass.Mapper.Tests/Pipelines/ObjectConstruction/Tasks/ObjectCachingSaver/ObjectCachingSaverTaskFixture.cs
View
5 Sites/Sitecore/Glass.Mapper.Sites.Sc/Glass.Mapper.Sites.Sc.ncrunchproject
@@ -17,9 +17,4 @@
<UseBuildPlatform></UseBuildPlatform>
<ProxyProcessPath></ProxyProcessPath>
<UseCPUArchitecture>AutoDetect</UseCPUArchitecture>
- <IgnoredTests>
- <RegexTestSelector>
- <RegularExpression>.*</RegularExpression>
- </RegexTestSelector>
- </IgnoredTests>
</ProjectConfiguration>
View
2  Source/Glass.Mapper.Sc/Caching/CacheKeyResolving/Implementations/SitecoreCacheKey.cs
@@ -16,7 +16,7 @@ public SitecoreCacheKey()
public override bool Equals(CacheKey<Guid> other)
{
- return other.RevisionId == this.RevisionId && other.Database == this.Database && other.Type == this.Type;
+ return other.Id.Equals(this.Id) && other.RevisionId.Equals(this.RevisionId) && other.Database == this.Database && other.Type == this.Type;
}
}
}
View
5 Source/Glass.Mapper.Sc/GlassConfig.cs
@@ -1,7 +1,4 @@
using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Glass.Mapper.Caching.CacheKeyResolving;
@@ -27,7 +24,7 @@
using Glass.Mapper.Caching.ObjectCaching.Implementations;
using Glass.Mapper.Sc.DataMappers.SitecoreQueryParameters;
-namespace Glass.Mapper.Sc.Integration
+namespace Glass.Mapper.Sc
{
public class GlassConfig : GlassCastleConfigBase
{
View
19 Source/Glass.Mapper/AbstractService.cs
@@ -38,10 +38,6 @@ public abstract class AbstractService<TK> : IAbstractService
/// </summary>
private IEnumerable<IObjectSavingTask> ObjectSavingTasks { get; set; }
- private AbstractObjectCacheConfiguration ObjectCacheConfiguration { get; set; }
-
-
-
public AbstractService()
: this(Context.Default)
{
@@ -63,7 +59,6 @@ public AbstractService(Context glassContext)
TypeResolverTasks = glassContext.DependencyResolver.ResolveAll<ITypeResolverTask>();
ConfigurationResolverTasks = glassContext.DependencyResolver.ResolveAll<IConfigurationResolverTask>();
ObjectSavingTasks = glassContext.DependencyResolver.ResolveAll<IObjectSavingTask>();
- ObjectCacheConfiguration = glassContext.DependencyResolver.Resolve<AbstractObjectCacheConfiguration>(new Dictionary<string, object> {{"glassContext", glassContext}});
}
public object InstantiateObject(AbstractTypeCreationContext abstractTypeCreationContext)
@@ -90,18 +85,8 @@ public object InstantiateObject(AbstractTypeCreationContext abstractTypeCreation
//Run the object construction
var objectRunner = new ObjectConstruction(ObjectConstructionTasks);
- ObjectConstructionArgs objectArgs;
- if (ObjectCacheConfiguration == null)
- {
- objectArgs = new ObjectConstructionArgs(GlassContext, abstractTypeCreationContext,
- config, this);
- }
- else
- {
- objectArgs = new ObjectConstructionArgs(GlassContext, abstractTypeCreationContext,
- config, this, ObjectCacheConfiguration);
- }
-
+ var objectArgs = new ObjectConstructionArgs(GlassContext, abstractTypeCreationContext,
+ config, this);
if (DisableCache)
{
using (new CacheDisabler(objectArgs))
View
5 Source/Glass.Mapper/Caching/CacheKeyResolving/CacheKey.cs
@@ -1,7 +1,4 @@
using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
namespace Glass.Mapper.Caching.CacheKeyResolving
{
@@ -27,7 +24,7 @@ protected CacheKey()
public override string ToString()
{
- return "{0},{1},{2}".Formatted(RevisionId, Database, Type);
+ return "{0},{1},{2},{3}".Formatted(Id, RevisionId, Database, Type);
}
public abstract bool Equals(CacheKey<TIdType> other);
View
64 Source/Glass.Mapper/Caching/ObjectCaching/AbstractObjectCache.cs
@@ -0,0 +1,64 @@
+using Glass.Mapper.Caching.CacheKeyResolving;
+using Glass.Mapper.Pipelines.ObjectConstruction;
+
+namespace Glass.Mapper.Caching.ObjectCaching
+{
+ public abstract class AbstractObjectCache<TIdType> : IAbstractObjectCache
+ {
+ public string BaseCacheKey { get; set; }
+
+ public string DefaultBaseCacheKey
+ {
+ get { return "GlassObjectCahe"; }
+ }
+
+ protected AbstractCacheKeyResolver<TIdType> CacheKeyResolver;
+
+ protected abstract object InternalGetObject(ObjectConstructionArgs args);
+ protected abstract bool InternalContansObject(ObjectConstructionArgs args);
+ protected abstract void InternalAddObject(ObjectConstructionArgs args);
+ protected abstract bool InternalClearCache();
+
+ public object GetObject(ObjectConstructionArgs args)
+ {
+ return InternalGetObject(args);
+ }
+
+ public bool ContansObject(ObjectConstructionArgs args)
+ {
+ return InternalContansObject(args);
+ }
+
+ public void AddObject(ObjectConstructionArgs args)
+ {
+ InternalAddObject(args);
+ }
+
+ public bool ClearCache()
+ {
+ return InternalClearCache();
+ }
+
+ protected AbstractObjectCache(string baseCacheKey)
+ {
+ BaseCacheKey = baseCacheKey;
+ }
+
+ protected AbstractObjectCache()
+ {
+ BaseCacheKey = DefaultBaseCacheKey;
+ }
+
+ protected AbstractObjectCache(Context glassContext)
+ : this()
+ {
+ CacheKeyResolver = glassContext.DependencyResolver.Resolve<AbstractCacheKeyResolver<TIdType>>();
+ }
+
+ protected AbstractObjectCache(AbstractCacheKeyResolver<TIdType> cacheKeyResolver)
+ : this()
+ {
+ CacheKeyResolver = cacheKeyResolver;
+ }
+ }
+}
View
4 Source/Glass.Mapper/Caching/ObjectCaching/IAbstractObjectCache.cs
@@ -4,9 +4,11 @@ namespace Glass.Mapper.Caching.ObjectCaching
{
public interface IAbstractObjectCache
{
+ string BaseCacheKey { get; set; }
+ string DefaultBaseCacheKey { get; }
object GetObject(ObjectConstructionArgs args);
bool ContansObject(ObjectConstructionArgs args);
- bool AddObject(ObjectConstructionArgs args);
+ void AddObject(ObjectConstructionArgs args);
bool ClearCache();
}
}
View
23 Source/Glass.Mapper/Caching/ObjectCaching/Implementations/CacheTable.cs
@@ -6,34 +6,39 @@ namespace Glass.Mapper.Caching.ObjectCaching.Implementations
{
public class CacheTable<TIdType> : AbstractObjectCache<TIdType>
{
- private static volatile Hashtable _table = new Hashtable();
+ private volatile Hashtable _table = new Hashtable();
+
+ public CacheTable():base()
+ {
+ }
public CacheTable(AbstractCacheKeyResolver<TIdType> cacheKeyResolver)
: base(cacheKeyResolver)
{
}
- public override object GetObject(Pipelines.ObjectConstruction.ObjectConstructionArgs args)
+ public CacheTable(string baseCacheKey)
+ : base(baseCacheKey)
+ {
+ }
+
+ protected override object InternalGetObject(Pipelines.ObjectConstruction.ObjectConstructionArgs args)
{
return _table[base.CacheKeyResolver.GetKey(args)];
}
- public override bool ContansObject(Pipelines.ObjectConstruction.ObjectConstructionArgs args)
+ protected override bool InternalContansObject(Pipelines.ObjectConstruction.ObjectConstructionArgs args)
{
return _table.ContainsKey(base.CacheKeyResolver.GetKey(args));
}
- public override bool AddObject(Pipelines.ObjectConstruction.ObjectConstructionArgs args)
+ protected override void InternalAddObject(Pipelines.ObjectConstruction.ObjectConstructionArgs args)
{
- if (args.Result == null)
- return false;
-
_table.Add(base.CacheKeyResolver.GetKey(args), args.Result);
- return true;
}
- public override bool ClearCache()
+ protected override bool InternalClearCache()
{
try
{
View
56 Source/Glass.Mapper/Caching/ObjectCaching/Implementations/MemoryCache.cs
@@ -0,0 +1,56 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.Caching;
+using System.Text;
+using Glass.Mapper.Caching.CacheKeyResolving;
+using Glass.Mapper.Pipelines.ObjectConstruction;
+
+namespace Glass.Mapper.Caching.ObjectCaching.Implementations
+{
+ public class MemoryCache<TIdType> : AbstractObjectCache<TIdType>
+ {
+ private volatile MemoryCache _memoryCache;
+
+ public MemoryCache():base()
+ {
+ _memoryCache = new MemoryCache(DefaultBaseCacheKey);
+ }
+
+ public MemoryCache(AbstractCacheKeyResolver<TIdType> cacheKeyResolver)
+ : base(cacheKeyResolver)
+ {
+ _memoryCache = new MemoryCache(DefaultBaseCacheKey);
+ }
+
+ public MemoryCache(string baseCacheKey)
+ : base(baseCacheKey)
+ {
+ _memoryCache = new MemoryCache(baseCacheKey);
+ }
+
+ protected override object InternalGetObject(ObjectConstructionArgs args)
+ {
+ throw new NotImplementedException();
+ }
+
+ protected override bool InternalContansObject(ObjectConstructionArgs args)
+ {
+ return _memoryCache.Contains(CacheKeyResolver.GetKey(args).ToString());
+ }
+
+ protected override void InternalAddObject(ObjectConstructionArgs args)
+ {
+ var policy = new CacheItemPolicy();
+ policy.SlidingExpiration = new TimeSpan(0, 2, 0,0);
+
+ _memoryCache.Set(CacheKeyResolver.GetKey(args).ToString(), args.Result, policy);
+
+ }
+
+ protected override bool InternalClearCache()
+ {
+ throw new NotImplementedException();
+ }
+ }
+}
View
31 Source/Glass.Mapper/Caching/ObjectCaching/ObjectCache.cs
@@ -1,31 +0,0 @@
-using Glass.Mapper.Caching.CacheKeyResolving;
-using Glass.Mapper.Pipelines.ObjectConstruction;
-
-namespace Glass.Mapper.Caching.ObjectCaching
-{
- public abstract class AbstractObjectCache<TIdType> : IAbstractObjectCache
- {
- protected AbstractCacheKeyResolver<TIdType> CacheKeyResolver;
-
- public abstract object GetObject(ObjectConstructionArgs args);
- public abstract bool ContansObject(ObjectConstructionArgs args);
- public abstract bool AddObject(ObjectConstructionArgs args);
- public abstract bool ClearCache();
-
- protected AbstractObjectCache()
- {
- }
-
- protected AbstractObjectCache(Context glassContext)
- {
- CacheKeyResolver = glassContext.DependencyResolver.Resolve<AbstractCacheKeyResolver<TIdType>>();
- }
-
-
-
- protected AbstractObjectCache(AbstractCacheKeyResolver<TIdType> cacheKeyResolver)
- {
- CacheKeyResolver = cacheKeyResolver;
- }
- }
-}
View
9 Source/Glass.Mapper/Caching/Proxy/CacheMethodInterceptor.cs
@@ -32,8 +32,14 @@ public void Intercept(IInvocation invocation)
string name = invocation.Method.Name.Substring(4);
//if the dictionary contains the name then a value must have been set
- if (_values.ContainsKey(name) && method == "get_")
+ if (method == "get_")
{
+ if (_values.ContainsKey(name))
+ {
+ invocation.ReturnValue = _values[name];
+ return;
+ }
+ _values[name] = invocation.Method.Invoke(_originalTarget, invocation.Arguments);
invocation.ReturnValue = _values[name];
return;
}
@@ -46,6 +52,7 @@ public void Intercept(IInvocation invocation)
}
}
+
invocation.ReturnValue = invocation.Method.Invoke(_originalTarget, invocation.Arguments);
}
View
21 Source/Glass.Mapper/CastleWindsor/CastleDependencyResolver.cs
@@ -1,4 +1,5 @@
-using System.Collections;
+using System;
+using System.Collections;
using Castle.MicroKernel;
using Castle.Windsor;
using System.Collections.Generic;
@@ -13,23 +14,27 @@ public class CastleDependencyResolver : IDependencyResolver
public T Resolve<T>(IDictionary<string, object> args = null)
{
+ if (args == null)
+ return _container.Resolve<T>();
+
+ return _container.Resolve<T>((IDictionary)args);
+ }
+
+ public T TryResolve<T>(IDictionary<string, object> args = null)
+ {
//TODO: Aaron This is bad need to look into a different way of doing this
try
{
- if (args == null)
- return _container.Resolve<T>();
-
- return _container.Resolve<T>((IDictionary)args);
+ return Resolve<T>(args);
}
- catch (ComponentNotFoundException ex)
+ catch (Exception ex)
{
- if (typeof (T) != typeof (AbstractObjectCacheConfiguration))
+ if (typeof(T) != typeof(AbstractObjectCacheConfiguration))
throw;
}
return default(T);
}
-
public void Load(string contextName, IGlassConfiguration config)
{
View
2  Source/Glass.Mapper/Configuration/AbstractObjectCacheConfiguration.cs
@@ -23,7 +23,7 @@ protected AbstractObjectCacheConfiguration(string contextName)
protected AbstractObjectCacheConfiguration(Context glassContext)
{
- ObjectCache = glassContext.DependencyResolver.Resolve<IAbstractObjectCache>(new Dictionary<string, object> { { "glassContext", glassContext } });
+ ObjectCache = glassContext.DependencyResolver.TryResolve<IAbstractObjectCache>();
}
}
}
View
9 Source/Glass.Mapper/Context.cs
@@ -24,8 +24,6 @@ public class Context
public static IDependencyResolverFactory ResolverFactory { get; set; }
-
-
static Context()
{
ResolverFactory = new CastleDependencyResolverFactory();
@@ -67,7 +65,9 @@ public static Context Create(IGlassConfiguration glassConfig, string contextName
var context = new Context();
context.DependencyResolver = ResolverFactory.GetResolver();
context.DependencyResolver.Load(contextName, glassConfig);
-
+
+ context.ObjectCacheConfiguration = context.DependencyResolver.TryResolve<AbstractObjectCacheConfiguration>();
+
Contexts[contextName] = context;
if (isDefault)
@@ -88,6 +88,8 @@ public static void Clear()
#endregion
+ public AbstractObjectCacheConfiguration ObjectCacheConfiguration { get; private set; }
+
/// <summary>
/// List of the type configurations loaded by this context
/// </summary>
@@ -142,7 +144,6 @@ private void ProcessProperties(IEnumerable<AbstractPropertyConfiguration> proper
foreach(var property in properties)
{
-
DataMapperResolverArgs args = new DataMapperResolverArgs(this, property);
args.PropertyConfiguration = property;
args.DataMappers = DependencyResolver.ResolveAll<AbstractDataMapper>();
View
4 Source/Glass.Mapper/Glass.Mapper.csproj
@@ -50,6 +50,7 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
+ <Reference Include="System.Runtime.Caching" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
@@ -68,7 +69,8 @@
<Compile Include="Caching\CacheKeyResolving\ICacheKey.cs" />
<Compile Include="Caching\ObjectCaching\IAbstractObjectCache.cs" />
<Compile Include="Caching\ObjectCaching\Implementations\CacheTable.cs" />
- <Compile Include="Caching\ObjectCaching\ObjectCache.cs" />
+ <Compile Include="Caching\ObjectCaching\Implementations\MemoryCache.cs" />
+ <Compile Include="Caching\ObjectCaching\AbstractObjectCache.cs" />
<Compile Include="Caching\Proxy\CacheInterfaceMethodInterceptor.cs" />
<Compile Include="Caching\Proxy\CacheMethodInterceptor.cs" />
<Compile Include="Caching\Proxy\CacheProxyGenerator.cs" />
View
1  Source/Glass.Mapper/IDependencyResolver.cs
@@ -8,6 +8,7 @@ namespace Glass.Mapper
public interface IDependencyResolver
{
T Resolve<T>(IDictionary<string, object> args = null);
+ T TryResolve<T>(IDictionary<string, object> args = null);
IEnumerable<T> ResolveAll<T>();
IEnumerable<T> ResolveAllInOrder<T>(string field);
void Load(string context, IGlassConfiguration config);
View
13 Source/Glass.Mapper/Pipelines/ObjectConstruction/ObjectConstructionArgs.cs
@@ -14,7 +14,7 @@ public class ObjectConstructionArgs : AbstractPipelineArgs
/// </summary>
public AbstractTypeConfiguration Configuration { get; private set; }
- public AbstractObjectCacheConfiguration ObjectCacheConfiguration { get; private set; }
+
public bool DisableCache { get; internal set; }
@@ -39,17 +39,6 @@ public ObjectConstructionArgs() : base()
Service = service;
}
- public ObjectConstructionArgs(
- Context context,
- AbstractTypeCreationContext abstractTypeCreationContext,
- AbstractTypeConfiguration configuration,
- IAbstractService service,
- AbstractObjectCacheConfiguration objectCacheConfiguration)
- : this(context, abstractTypeCreationContext, configuration, service)
- {
- ObjectCacheConfiguration = objectCacheConfiguration;
- }
-
View
2  Source/Glass.Mapper/Pipelines/ObjectConstruction/Tasks/CreateConcrete/CreateConcreteTask.cs
@@ -47,7 +47,7 @@ public override void Execute(ObjectConstructionArgs args)
args.ObjectOrigin = ObjectOrigin.CreateConcrete;
}
- if (args.DisableCache || args.ObjectCacheConfiguration == null)
+ if (args.DisableCache || args.Context.ObjectCacheConfiguration == null)
{
args.AbortPipeline();
}
View
9 Source/Glass.Mapper/Pipelines/ObjectConstruction/Tasks/ObjectCachingResolver/ObjectCachingResolverTask.cs
@@ -12,14 +12,11 @@ public override void Execute(ObjectConstructionArgs args)
{
if (args.DisableCache) return;
- if (args.ObjectCacheConfiguration == null) return;
+ if (args.Context.ObjectCacheConfiguration == null) return;
- if (!args.ObjectCacheConfiguration.ObjectCache.ContansObject(args)) return;
+ if (!args.Context.ObjectCacheConfiguration.ObjectCache.ContansObject(args)) return;
- args.Result =
- args.Result =
- CacheProxyGenerator.CreateProxy(
- args.ObjectCacheConfiguration.ObjectCache.GetObject(args));
+ args.Result = args.Context.ObjectCacheConfiguration.ObjectCache.GetObject(args);
args.ObjectOrigin = ObjectOrigin.ObjectCachingResolver;
args.AbortPipeline();
}
View
2  Source/Glass.Mapper/Pipelines/ObjectConstruction/Tasks/ObjectCachingSaver/ObjectCachingSaverTask.cs
@@ -13,7 +13,7 @@ public override void Execute(ObjectConstructionArgs args)
if (args.DisableCache) return;
//Save item to the cache
- args.ObjectCacheConfiguration.ObjectCache.AddObject(args);
+ args.Context.ObjectCacheConfiguration.ObjectCache.AddObject(args);
args.Result = CacheProxyGenerator.CreateProxy(args.Result);
}
View
8 Tests/Integration Tests/Sitecore/Glass.Mapper.Sc.Integration/SitecoreServiceFixture.cs
@@ -527,10 +527,10 @@ public interface IStubSaving
[SitecoreType]
public class StubClass{
- public DateTime Param3 { get; set; }
- public bool Param4 { get; set; }
- public string Param2 { get; set; }
- public int Param1 { get; set; }
+ public virtual DateTime Param3 { get; set; }
+ public virtual bool Param4 { get; set; }
+ public virtual string Param2 { get; set; }
+ public virtual int Param1 { get; set; }
public StubClass()
{
View
2  Tests/Unit Tests/Glass.Mapper.Sc.Tests/Caching/SitecoreCacheKeyResolverFixture.cs
@@ -51,7 +51,7 @@ public void CanGenerateCacheKey()
//Assert
- Assert.AreEqual(new CacheKey(revistionId, "master", type), cacheKey);
+ Assert.AreEqual(new SitecoreCacheKey(sitecoreItem.ID.Guid, revistionId, "master", type), cacheKey);
}
#region Stubs
View
828 Tests/Unit Tests/Glass.Mapper.Tests/Caching/AbstractObjectCacheFixture.cs
@@ -0,0 +1,828 @@
+using System;
+using System.Threading.Tasks;
+using Glass.Mapper.Caching.CacheKeyResolving;
+using Glass.Mapper.Caching.ObjectCaching;
+using Glass.Mapper.Caching.ObjectCaching.Implementations;
+using Glass.Mapper.Pipelines.ObjectConstruction;
+using NSubstitute;
+using NUnit.Framework;
+
+namespace Glass.Mapper.Tests.Caching
+{
+ [TestFixture]
+ public class ObjectCachingFixture
+ {
+ private CacheKey<int> _cacheKey;
+ private ObjectConstructionArgs _args;
+ private AbstractCacheKeyResolver<int> _cacheKeyResolver;
+
+ [SetUp]
+ public void SetUp()
+ {
+ _cacheKey = Substitute.For<CacheKey<int>>();
+ _args = Substitute.For<ObjectConstructionArgs>();
+ _cacheKeyResolver = Substitute.For<AbstractCacheKeyResolver<int>>();
+ _cacheKeyResolver.GetKey(_args).Returns(_cacheKey);
+ }
+
+ [Test]
+ [TestCase(typeof(CacheTable<int>))]
+ [TestCase(typeof(MemoryCache<int>))]
+ public void Default_Constructor_Sets_Default_BaseCacheKey(Type type)
+ {
+ var objectCache = (IAbstractObjectCache)System.Activator.CreateInstance(type);
+
+ Assert.AreEqual(objectCache.DefaultBaseCacheKey, objectCache.BaseCacheKey);
+ }
+
+ [Test]
+ [TestCase(typeof(CacheTable<int>))]
+ [TestCase(typeof(MemoryCache<int>))]
+ public void Can_Set_BaseCacheKey_With_Constructor(Type type)
+ {
+ var baseKey = "Can_Set_BaseCacheKey_With_Constructor";
+
+ var objectCache = (IAbstractObjectCache)System.Activator.CreateInstance(type, new object[] { baseKey });
+
+ Assert.AreEqual(baseKey, objectCache.BaseCacheKey);
+ }
+
+
+ [Test]
+ [TestCase(typeof(CacheTable<int>))]
+ [TestCase(typeof(MemoryCache<int>))]
+ public void Can_Add_Object(Type type)
+ {
+ var objectCache = (IAbstractObjectCache)System.Activator.CreateInstance(type, new object[] { _cacheKeyResolver });
+
+ var test = new StubClass();
+ test.MyProperty = "Can_Add_Object";
+ var templateString = "templateString";
+ var key = "Can_Add_ObjectKey";
+
+ _args.Result.Returns(test);
+
+ _cacheKey.ToString().Returns(key);
+
+ objectCache.AddObject(_args);
+ Assert.IsTrue(objectCache.ContansObject(_args));
+ }
+
+
+ //[Test]
+ //[TestCase(typeof(CacheTable<int>))]
+ //[TestCase(typeof(MemoryCache<int>))]
+ //public void Does_Remember_Template(Type type)
+ //{
+ // var baseKey = "Does_Remember_Template";
+
+ // var objectCache = (IAbstractObjectCache)System.Activator.CreateInstance(type, new[] { baseKey });
+
+ // var test = new Test();
+ // test.MyProperty = "Can_Add_Object";
+ // var templateString = "templateString";
+ // var key = "CreateKey";
+
+ // objectCache.Add<Test>(key, test, templateString, ObjectCachePriority.High);
+
+ // Assert.IsTrue(objectCache.GetKeys(templateString, ObjectCachePriority.High).Any());
+ //}
+
+ [Test]
+ [TestCase(typeof(CacheTable<int>))]
+ [TestCase(typeof(MemoryCache<int>))]
+ public void Can_Add_Thread_Safe(Type type)
+ {
+ var baseKey = "Can_Add_Thread_Safe";
+
+ var cacheKeyResolver = Substitute.For<AbstractCacheKeyResolver<int>>();
+ var objectCache = (IAbstractObjectCache)System.Activator.CreateInstance(type, new[] { cacheKeyResolver });
+ var templateString = "templateString";
+ var key = "Can_Add_Thread_Safe";
+
+ Parallel.For(0, 10, i =>
+ {
+ for (int j = 0; j < 100; j++)
+ {
+ var test = new StubClass();
+ test.MyProperty = key + j + i;
+
+ var cacheKey = Substitute.For<CacheKey<int>>();
+
+ var args = Substitute.For<ObjectConstructionArgs>();
+ cacheKey.ToString().Returns(x => key + j + i);
+ cacheKeyResolver.GetKey(args).Returns(x => cacheKey);
+ cacheKeyResolver.GetKey(args).ToString().Returns(x => key + j + i);
+
+
+ args.Result = test;
+
+
+ objectCache.AddObject(args);
+
+ Assert.IsTrue(objectCache.ContansObject(args));
+ }
+ });
+ }
+
+
+ //[Test]
+ //[TestCase(typeof(CacheTable<int>))]
+ //[TestCase(typeof(MemoryCache<int>))]
+ //public void Can_Add_Object_To_Multiple_Releated_Cache(Type type)
+ //{
+ // var baseKey = "Can_Add_Object_To_Multiple_Releated_Cache";
+
+ // var objectCache = (IAbstractObjectCache)System.Activator.CreateInstance(type, new[] { baseKey });
+
+ // var test = new Test();
+ // test.MyProperty = "Can_Add_Object";
+ // var templateStrings = new List<string>() {"templateString1", "templateString2"};
+
+ // var key = "Can_Add_Object_To_Multiple_Releated_Cache";
+
+ // objectCache.Add<Test>(key, test, templateStrings, ObjectCachePriority.High);
+
+ // Assert.AreEqual(test, objectCache.Get<Test>(key));
+ //}
+
+ // [Test]
+ // [TestCase(typeof(CacheTable<int>))]
+ // [TestCase(typeof(MemoryCache<int>))]
+ // public void Can_Add_Object_To_Multiple_Releated_Cache_Thread_Safe(Type type)
+ // {
+ // var baseKey = "Can_Add_Object_To_Multiple_Releated_Cache_Thread_Safe";
+
+ // var objectCache = (IAbstractObjectCache)System.Activator.CreateInstance(type, new[] { baseKey });
+
+ // var test = new Test();
+ // test.MyProperty = "Can_Add_Object";
+ // var templateStrings = new List<string>() { "templateString1", "templateString2" };
+
+ // var key = "Can_Add_Object_To_Multiple_Releated_Cache_Thread_Safe";
+
+ // Parallel.For(0, 10, i =>
+ // {
+ // for (int j = 0; j < 100; j++)
+ // {
+ // objectCache.Add<Test>(key + i + j, test, templateStrings, ObjectCachePriority.High);
+
+ // Assert.AreEqual(test, objectCache.Get<Test>(key + i + j));
+ // }
+ // });
+ // }
+
+
+ // [Test]
+ // [TestCase(typeof(CacheTable<int>))]
+ // [TestCase(typeof(MemoryCache<int>))]
+ // public void Can_Clear_Object_In_Multiple_Releated_Cache(Type type)
+ // {
+ // var baseKey = "Can_Clear_Object_In_Multiple_Releated_Cache";
+
+ // var objectCache = (IAbstractObjectCache)System.Activator.CreateInstance(type, new[] { baseKey });
+
+ // var test = new Test();
+ // test.MyProperty = "Can_Add_Object";
+ // var templateStrings = new List<string>() { "templateString1", "templateString2" };
+
+ // var key = "Can_Clear_Object_In_Multiple_Releated_Cache";
+
+ // objectCache.Add<Test>(key, test, templateStrings, ObjectCachePriority.High);
+
+ // Assert.AreEqual(test, objectCache.Get<Test>(key));
+ // Assert.IsTrue(objectCache.ClearRelatedCache(templateStrings[0], ObjectCachePriority.High));
+ // Assert.IsTrue(objectCache.Get<Test>(key) == null);
+
+ // objectCache.Add<Test>(key, test, templateStrings, ObjectCachePriority.High);
+
+ // Assert.AreEqual(objectCache.Get<Test>(key), test);
+ // Assert.IsTrue(objectCache.ClearRelatedCache(templateStrings[1], ObjectCachePriority.High));
+ // Assert.IsTrue(objectCache.Get<Test>(key) == null);
+ // }
+
+
+ // [Test]
+ // [TestCase(typeof(CacheTable<int>))]
+ // [TestCase(typeof(MemoryCache<int>))]
+ // public void Can_Clear_Object_In_Multiple_Releated_Cache_Thread_Safe(Type type)
+ // {
+ // var baseKey = "Can_Add_Object_To_Multiple_Releated_Cache_Thread_Safe";
+
+ // var objectCache = (IAbstractObjectCache)System.Activator.CreateInstance(type, new[] { baseKey });
+
+ // var test = new Test();
+ // test.MyProperty = "Can_Add_Object";
+
+ // var key = "Can_Add_Object_To_Multiple_Releated_Cache_Thread_Safe";
+
+ // Parallel.For(0, 10, i =>
+ // {
+ // for (int j = 0; j < 100; j++)
+ // {
+ // var templateStrings = new List<string>() { "templateString1" + i, "templateString2" + i };
+
+ // objectCache.Add<Test>(key + i + j, test, templateStrings, ObjectCachePriority.High);
+
+ // Assert.AreEqual(test, objectCache.Get<Test>(key + i + j));
+ // Assert.IsTrue(objectCache.ClearRelatedCache(templateStrings[0], ObjectCachePriority.High));
+ // Assert.IsNull(objectCache.Get<Test>(key + i + j));
+
+ // objectCache.Add<Test>(key + i + j, test, templateStrings, ObjectCachePriority.High);
+
+ // Assert.AreEqual(test, objectCache.Get<Test>(key + i + j));
+ // Assert.IsTrue(objectCache.ClearRelatedCache(templateStrings[1], ObjectCachePriority.High));
+ // Assert.IsTrue(objectCache.Get<Test>(key + i + j) == null);
+ // }
+ // });
+ // }
+
+
+ // [Test]
+ // [TestCase(typeof(CacheTable<int>))]
+ // [TestCase(typeof(MemoryCache<int>))]
+ // public void Can_Clear_Releated_Cache_With_Multiple_Object_In_Multiple_Releated_Cache(Type type)
+ // {
+ // var baseKey = "Can_Clear_Releated_Cache_With_Multiple_Object_In_Multiple_Releated_Cache";
+
+ // var objectCache = (IAbstractObjectCache)System.Activator.CreateInstance(type, new[] { baseKey });
+
+ // var test = new Test();
+ // test.MyProperty = "Can_Add_Object1";
+
+ // var test2 = new Test();
+ // test.MyProperty = "Can_Add_Object2";
+
+ // var templateStrings = new List<string>() { "templateString1", "templateString2" };
+
+ // var key = "Can_Clear_Object_In_Multiple_Releated_Cache";
+ // var key2 = "Can_Clear_Object_In_Multiple_Releated_Cache2";
+
+ // objectCache.Add<Test>(key, test, templateStrings, ObjectCachePriority.High);
+ // objectCache.Add<Test>(key2, test2, templateStrings, ObjectCachePriority.High);
+
+ // Assert.AreEqual(objectCache.Get<Test>(key), test);
+ // Assert.IsTrue(objectCache.ClearRelatedCache(templateStrings[0], ObjectCachePriority.High));
+ // Assert.IsNull(objectCache.Get<Test>(key));
+ // Assert.IsNull(objectCache.Get<Test>(key2));
+ // }
+
+
+ // [Test]
+ // [TestCase(typeof(CacheTable<int>))]
+ // [TestCase(typeof(MemoryCache<int>))]
+ // public void Does_Clearing_One_Releated_Cache_Clear_Another(Type type)
+ // {
+ // var baseKey = "Does_Clearing_One_Releated_Cache_Clear_Another";
+
+ // var objectCache = (IAbstractObjectCache)System.Activator.CreateInstance(type, new[] { baseKey });
+
+ // var test = new Test();
+ // test.MyProperty = "Can_Add_Object1";
+
+ // var test2 = new Test();
+ // test.MyProperty = "Can_Add_Object2";
+
+ // var templateStrings = new List<string>() { "templateString1", "templateString2" };
+
+ // var key = "Can_Clear_Object_In_Multiple_Releated_Cache";
+ // var key2 = "Can_Clear_Object_In_Multiple_Releated_Cache2";
+
+ // objectCache.Add<Test>(key, test, templateStrings, ObjectCachePriority.High);
+ // objectCache.Add<Test>(key2, test2, templateStrings, ObjectCachePriority.High);
+
+ // Assert.IsNotNull(objectCache.GetReleatedKeys(ObjectCachePriority.High));
+ // Assert.AreEqual(templateStrings[0], objectCache.GetReleatedKeys(ObjectCachePriority.High).First().Key);
+
+ // Assert.IsTrue(objectCache.ClearRelatedCache(templateStrings[0], ObjectCachePriority.High));
+
+ // Assert.IsNotNull(objectCache.GetReleatedKeys(ObjectCachePriority.High));
+
+ // Assert.IsNull(objectCache.Get<Test>(key));
+ // Assert.IsNull(objectCache.Get<Test>(key2));
+
+ // //Assert.IsFalse(objectCache.GetReleatedKeys(ObjectCachePriority.High).Keys.con);
+
+ // }
+
+
+ // [Test]
+ // [TestCase(typeof(CacheTable<int>))]
+ // [TestCase(typeof(MemoryCache<int>))]
+ // public void Can_Clear_Releated_Cache(Type type)
+ // {
+ // var baseKey = "Can_Clear_Releated_Cache";
+
+ // var objectCache = (IAbstractObjectCache)System.Activator.CreateInstance(type, new[] { baseKey });
+
+ // var test = new Test();
+ // test.MyProperty = "Can_Add_Object";
+ // var templateString = "templateString";
+ // var key = "Can_Clear_Releated_CacheKey";
+
+ // objectCache.Add<Test>(key, test, templateString, ObjectCachePriority.High);
+
+ // Assert.AreEqual(objectCache.Get<Test>(key), test);
+ // Assert.IsTrue(objectCache.ClearRelatedCache(templateString, ObjectCachePriority.High));
+ // Assert.IsTrue(objectCache.Get<Test>(key) == null);
+ // }
+
+ // [Test]
+ // [TestCase(typeof(CacheTable<int>))]
+ // [TestCase(typeof(MemoryCache<int>))]
+ // public void Can_Clear_Releated_Thread_Safe(Type type)
+ // {
+ // var baseKey = "Can_Clear_Releated_Thread_Safe";
+
+ // var objectCache = (IAbstractObjectCache)System.Activator.CreateInstance(type, new[] { baseKey });
+
+ // var templateString = "Can_Clear_Releated_Thread_SafetemplateString";
+ // var key = "Can_Clear_Releated_Thread_Safe";
+
+ // Parallel.For(0, 10, i =>
+ // {
+ // for (int j = 0; j < 100; j++)
+ // {
+ // var test = new Test();
+ // test.MyProperty = "Can_Clear_Releated_Thread_Safe";
+
+ // objectCache.Add<Test>(key + i + j, test, templateString + i, ObjectCachePriority.High);
+
+ // Assert.AreEqual(test, objectCache.Get<Test>(key + i + j));
+ // Assert.IsTrue(objectCache.ClearRelatedCache(templateString + i, ObjectCachePriority.High));
+ // Assert.IsNull(objectCache.Get<Test>(key + i + j));
+ // }
+ // });
+ // }
+
+ // [Test]
+ // [TestCase(typeof(CacheTable<int>))]
+ // [TestCase(typeof(MemoryCache<int>))]
+ // public void Can_Get_Object_By_Key(Type type)
+ // {
+ // var baseKey = "Can_Get_Object_By_Key";
+
+ // var objectCache = (IAbstractObjectCache)System.Activator.CreateInstance(type, new[] { baseKey });
+
+ // var templateString = "templateString";
+ // var key = "Cant_Add_NullKey";
+
+ // var test = new Test();
+ // test.MyProperty = "Can_Add_Object";
+
+ // objectCache.Add<Test>(key, test, templateString, ObjectCachePriority.High);
+
+ // Assert.AreEqual(test, objectCache.Get<Test>(key));
+ // }
+
+ // [Test]
+ // [TestCase(typeof(CacheTable<int>))]
+ // [TestCase(typeof(MemoryCache<int>))]
+ // public void Can_Get_Object_By_Key_Thread_Safe(Type type)
+ // {
+ // var baseKey = "Can_Get_Object_By_Key_Thread_Safe";
+
+ // var objectCache = (IAbstractObjectCache)System.Activator.CreateInstance(type, new[] { baseKey });
+
+ // var templateString = "templateString";
+ // var key = "CreateKey";
+
+ // Parallel.For(0, 10, i =>
+ // {
+ // for (int j = 0; j < 100; j++)
+ // {
+ // var test = new Test();
+ // test.MyProperty = "Can_Add_Object";
+
+ // objectCache.Add<Test>(key + i + j, test, templateString, ObjectCachePriority.High);
+
+ // Assert.AreEqual(test, objectCache.Get<Test>(key + i + j));
+ // }
+ // });
+ // }
+
+
+ // [Test]
+ // [TestCase(typeof(CacheTable<int>))]
+ // [TestCase(typeof(MemoryCache<int>))]
+ // public void Can_Remove_Object_By_Key(Type type)
+ // {
+ // var baseKey = "Can_Remove_Object_By_Key";
+
+ // var objectCache = (IAbstractObjectCache)System.Activator.CreateInstance(type, new[] { baseKey });
+
+ // var templateString = "templateString";
+ // var key = "Cant_Add_NullKey";
+
+ // var test = new Test();
+ // test.MyProperty = "Can_Add_Object";
+
+ // objectCache.Add<Test>(key, test, templateString, ObjectCachePriority.High);
+
+ // Assert.AreEqual(objectCache.Get<Test>(key), test);
+
+ // objectCache.Remove(key);
+
+ // Assert.IsNull(objectCache.Get<Test>(key));
+ // Assert.IsNotNull(objectCache.GetReleatedKeys(ObjectCachePriority.High));
+ // Assert.IsFalse(objectCache.GetReleatedKeys(ObjectCachePriority.High).ContainsKey(templateString));
+ // }
+
+
+ // [Test]
+ // [TestCase(typeof(CacheTable<int>))]
+ // [TestCase(typeof(MemoryCache<int>))]
+ // public void Can_Remove_Object_By_Key_Thread_Safe(Type type)
+ // {
+ // var baseKey = "Can_Remove_Object_By_Key_Thread_Safe";
+
+ // var objectCache = (IAbstractObjectCache)System.Activator.CreateInstance(type, new[] { baseKey });
+
+ // var templateString = "templateString";
+ // var key = "Can_Remove_Object_By_Key_Thread_SafeKey";
+
+ // Parallel.For(0, 10, i =>
+ // {
+ // for (int j = 0; j < 100; j++)
+ // {
+ // var test = new Test();
+ // test.MyProperty = "Can_Add_Object";
+
+ // objectCache.Add<Test>(key + i + j, test, templateString, ObjectCachePriority.High);
+
+ // Assert.AreEqual(test, objectCache.Get<Test>(key + i + j));
+
+ // //objectCache.Remove(key + i + j);
+
+ // //Assert.IsNull(objectCache.Get<Test>(key + i + j));
+ // }
+ // });
+ // }
+
+
+ // [Test]
+ // [TestCase(typeof(CacheTable<int>))]
+ // [TestCase(typeof(MemoryCache<int>))]
+ // public void Get_ObjectCachePriority_ReleatedKeys(Type type)
+ // {
+ // var baseKey = "Get_ObjectCachePriority_ReleatedKeys";
+
+ // var objectCache = (IAbstractObjectCache)System.Activator.CreateInstance(type, new[] { baseKey });
+
+ // var templateString = "templateString";
+ // var key = "Cant_Add_NullKey";
+
+ // var test = new Test();
+ // test.MyProperty = "Can_Add_Object";
+
+ // objectCache.Add<Test>(key, test, templateString, ObjectCachePriority.High);
+
+ // Assert.IsNotNull(objectCache.GetReleatedKeys(ObjectCachePriority.High));
+ // Assert.AreEqual(templateString, objectCache.GetReleatedKeys(ObjectCachePriority.High).First().Key);
+ // }
+
+ // [Test]
+ // [ExpectedException(typeof(DuplicatedKeyObjectCacheException))]
+ // [TestCase(typeof(CacheTable<int>))]
+ // [TestCase(typeof(MemoryCache<int>))]
+ // public void Cant_Add_Two_Objects_With_Same_Key(Type type)
+ // {
+ // var baseKey = "Cant_Add_Two_Objects_With_Same_Key";
+
+ // var objectCache = (IAbstractObjectCache)System.Activator.CreateInstance(type, new[] { baseKey });
+
+ // var templateString = "templateString";
+ // var key = "Cant_Add_NullKey";
+
+ // var test = new Test();
+ // test.MyProperty = "Can_Add_Object";
+
+ // var test1 = new Test();
+ // test.MyProperty = "Can_Add_Object";
+
+ // Assert.IsTrue(objectCache.Add<Test>(key, test, templateString, ObjectCachePriority.High));
+ // Assert.IsFalse(objectCache.Add<Test>(key, test1, templateString, ObjectCachePriority.High));
+ // }
+
+ // [Test]
+ // [TestCase(typeof(CacheTable<int>))]
+ // [TestCase(typeof(MemoryCache<int>))]
+ // public void Clearing_Differnt_Releated_Cache_Wont_Remove_Object(Type type)
+ // {
+ // var baseKey = "Clearing_Differnt_Releated_Cache_Wont_Remove_Object";
+
+ // var objectCache = (IAbstractObjectCache)System.Activator.CreateInstance(type, new[] { baseKey });
+
+ // var templateString = "templateString";
+ // var key = "Clearing_Differnt_Releated_Cache_Wont_Remove_Object";
+
+ // var test = new Test();
+ // test.MyProperty = "Can_Add_Object";
+
+ // objectCache.Add<Test>(key, test, templateString, ObjectCachePriority.High);
+
+ // Assert.AreSame(test, objectCache.Get<Test>(key));
+ // Assert.IsTrue(objectCache.GetReleatedKeys(ObjectCachePriority.High).Any(x => x.Key == templateString));
+
+ // Assert.IsTrue(objectCache.ClearRelatedCache("Another_Clearing_Differnt_Releated_Cache_Wont_Remove_Object", ObjectCachePriority.High));
+
+ // Assert.IsTrue(objectCache.GetReleatedKeys(ObjectCachePriority.High).Any(x => x.Key == templateString));
+
+ // }
+
+ // [Test]
+ // [TestCase(typeof(CacheTable<int>))]
+ // [TestCase(typeof(MemoryCache<int>))]
+ // public void Add_To_Cache_Clear_Releated_And_Readd(Type type)
+ // {
+ // var baseKey = "Add_To_Cache_Clear_Releated_And_Readd";
+
+ // var objectCache = (IAbstractObjectCache)System.Activator.CreateInstance(type, new[] { baseKey });
+
+ // var templateString = "templateString";
+ // var key = "Cant_Add_NullKey";
+
+ // var test = new Test();
+ // test.MyProperty = "Can_Add_Object";
+
+ // objectCache.Add<Test>(key, test, templateString, ObjectCachePriority.High);
+
+ // Assert.AreSame(test, objectCache.Get<Test>(key));
+ // Assert.IsTrue(objectCache.GetReleatedKeys(ObjectCachePriority.High).Any(x => x.Key == templateString));
+
+ // Assert.IsTrue(objectCache.ClearRelatedCache(templateString, ObjectCachePriority.High));
+
+ // objectCache.Add<Test>(key, test, templateString, ObjectCachePriority.High);
+
+ // Assert.AreSame(test, objectCache.Get<Test>(key));
+ // Assert.IsTrue(objectCache.GetReleatedKeys(ObjectCachePriority.High).Any(x => x.Key == templateString));
+
+ // }
+
+
+ // [Test]
+ // [TestCase(typeof(CacheTable<int>))]
+ // [TestCase(typeof(MemoryCache<int>))]
+ // public void Add_To_Cache_Clear_Releated_And_Readd_Thread_Safe(Type type)
+ // {
+ // var baseKey = "Add_To_Cache_Clear_Releated_And_Readd_Thread_Safe";
+
+ // var objectCache = (IAbstractObjectCache)System.Activator.CreateInstance(type, new[] { baseKey });
+
+ // var templateString = "templateString";
+ // var key = "Add_To_Cache_Clear_Releated_And_Readd_Thread_Safe";
+
+ // var test = new Test();
+ // test.MyProperty = "Can_Add_Object";
+
+ // Parallel.For(0, 2, i =>
+ // {
+ // objectCache.Add<Test>(key + i, test, templateString + i, ObjectCachePriority.High);
+
+ // Assert.AreSame(test, objectCache.Get<Test>(key + i));
+ // Assert.IsTrue(objectCache.DoesReleatedTemplateIDExist(templateString + i, ObjectCachePriority.High));
+
+ // Assert.IsTrue(objectCache.ClearRelatedCache(templateString + i, ObjectCachePriority.High));
+
+ // objectCache.Add<Test>(key + i, test, templateString + i, ObjectCachePriority.High);
+
+ // Assert.AreSame(test, objectCache.Get<Test>(key + i));
+ // Assert.IsTrue(objectCache.DoesReleatedTemplateIDExist(templateString + i, ObjectCachePriority.High));
+
+ // });
+
+ // }
+
+ // [Test]
+ // [ExpectedException(typeof(KeyNullOrEmptyObjectCacheException))]
+ // [TestCase(typeof(CacheTable<int>))]
+ // [TestCase(typeof(MemoryCache<int>))]
+ // public void Does_Empty_Key_Fail(Type type)
+ // {
+ // var baseKey = "Does_Empty_Key_Fail";
+
+ // var objectCache = (IAbstractObjectCache)System.Activator.CreateInstance(type, new[] { baseKey });
+
+ // var templateString = "templateString";
+ // var key = "";
+
+ // var test = new Test();
+ // test.MyProperty = "Does_Empty_Key_Fail";
+
+ // Assert.IsFalse(objectCache.Add<Test>(key, test, templateString, ObjectCachePriority.High));
+ // }
+
+
+ // [Test]
+ // [TestCase(typeof(CacheTable<int>))]
+ // [TestCase(typeof(MemoryCache<int>))]
+ // public void Can_Add_Colection(Type type)
+ // {
+ // var baseKey = "Can_Add_Colection";
+
+ // var objectCache = (IAbstractObjectCache)System.Activator.CreateInstance(type, new[] { baseKey });
+
+ // var templateString = "templateString";
+ // var key = "Can_Add_Colection";
+
+ // var list = new List<string>() { "1", "2", "3", "4", "5" };
+
+ // Assert.IsTrue(objectCache.Add<List<string>>(key, list, templateString, ObjectCachePriority.High));
+ // Assert.AreEqual(list, objectCache.Get<List<string>>(key));
+ // }
+
+ // [Test]
+ // [TestCase(typeof(CacheTable<int>))]
+ // [TestCase(typeof(MemoryCache<int>))]
+ // public void Can_Rebuild_On_Remove(Type type)
+ // {
+ // var baseKey = "Can_Add_Colection";
+
+ // var objectCache = (IAbstractObjectCache)System.Activator.CreateInstance(type, new[] { baseKey });
+
+ // var templateString = "templateString";
+ // var key = "Can_Add_Colection";
+
+ // Func<List<string>> buildList = delegate()
+ // {
+ // return new List<string>() { "1", "2", "3", "4", "5" };
+ // };
+
+ // var list = buildList.Invoke();
+
+ // Assert.IsTrue(objectCache.Add<List<string>>(key, list, templateString, ObjectCachePriority.High, buildList));
+ // Assert.AreEqual(list, objectCache.Get<List<string>>(key));
+
+ // objectCache.Remove(key);
+
+ // Assert.AreEqual(list, objectCache.Get<List<string>>(key));
+ // }
+
+ // [Test]
+ // [TestCase(typeof(CacheTable<int>))]
+ // [TestCase(typeof(MemoryCache<int>))]
+ // public void Will_Rebuild_On_Remove(Type type)
+ // {
+ // var baseKey = "Can_Add_Colection";
+
+ // var objectCache = (IAbstractObjectCache)System.Activator.CreateInstance(type, new[] { baseKey });
+
+ // var templateString = "templateString";
+ // var key = "Can_Add_Colection";
+
+ // Func<List<string>> buildList = delegate()
+ // {
+ // return new List<string>() { "1", "2", "3", "4", "5" };
+ // };
+
+ // var list = buildList.Invoke();
+
+ // Assert.IsTrue(objectCache.Add<List<string>>(key, list, templateString, ObjectCachePriority.High, buildList));
+
+ // Assert.IsTrue(objectCache.WillRebuildOnRemove(key));
+
+ // }
+
+
+ // [Test]
+ // [TestCase(typeof(CacheTable<int>))]
+ //// [TestCase(typeof(MemoryCache<int>))]
+ // public void Can_Add_Object_With_Custom_Timeout(Type type)
+ // {
+ // var baseKey = "Can_Add_Object_With_Custom_Timeout";
+
+ // var objectCache = (IAbstractObjectCache)System.Activator.CreateInstance(type, new[] { baseKey });
+
+ // var templateString = "templateString";
+ // var key = "Can_Add_Object_With_Custom_Timeout";
+
+ // var test = new Test();
+ // test.MyProperty = "Can_Add_Object_With_Custom_Timeout";
+
+ // var ts = new TimeSpan(0, 0, 5);
+
+ // objectCache.Add<Test>(key, test, templateString, ts);
+
+ // Assert.AreEqual(test, objectCache.Get<Test>(key));
+ // }
+
+ // [Test]
+ // [TestCase(typeof(CacheTable<int>))]
+ // [TestCase(typeof(MemoryCache<int>))]
+ // public void Can_Add_Object_With_Custom_Timeout_Will_Expire(Type type)
+ // {
+ // var baseKey = "Can_Add_Object_With_Custom_Timeout_Will_Expire";
+
+ // var objectCache = (IAbstractObjectCache)System.Activator.CreateInstance(type, new[] { baseKey });
+
+ // var templateString = "templateString";
+ // var key = "Can_Add_Object_With_Custom_Timeout_Will_Expire";
+
+ // var test = new Test();
+ // test.MyProperty = "Can_Add_Object_With_Custom_Timeout_Will_Expire";
+
+ // var ts = new TimeSpan(0, 0, 1);
+
+ // objectCache.Add<Test>(key, test, templateString, ts);
+
+ // //sleep some so the expiry is after now
+ // Thread.Sleep(2000);
+
+ // Assert.IsNull(objectCache.Get<Test>(key));
+ // }
+
+
+ // [Test]
+ // public void Can_Parse_String_To_ObjectCachePriority(Type type)
+ // {
+ // Assert.AreEqual(ObjectCachePriority.High, ObjectCache.ParseObjectCachePriority("High"));
+ // Assert.AreEqual(ObjectCachePriority.Medium, ObjectCache.ParseObjectCachePriority("Medium"));
+ // Assert.AreEqual(ObjectCachePriority.Low, ObjectCache.ParseObjectCachePriority("Low"));
+ // Assert.AreEqual(ObjectCachePriority.Custom, ObjectCache.ParseObjectCachePriority("Custom"));
+ // }
+
+ // [Test]
+ // [TestCase(typeof(CacheTable<int>))]
+ // [TestCase(typeof(MemoryCache<int>))]
+ // public void Can_Turn_Cache_Off(Type type)
+ // {
+ // var baseKey = "Can_Turn_Cache_Off";
+
+ // var objectCache = (IAbstractObjectCache)System.Activator.CreateInstance(type, new[] { baseKey });
+
+ // var templateString = "templateString";
+ // var key = "Type type";
+
+ // var test = new Test();
+ // test.MyProperty = "Can_Turn_Cache_Off";
+
+ // Assert.IsTrue(objectCache.UseCache);
+
+ // objectCache.TurnCacheOff();
+
+ // Assert.IsFalse(objectCache.UseCache);
+
+ // objectCache.Add<Test>(key, test, templateString, ObjectCachePriority.High);
+
+ // Assert.IsNull(objectCache.Get<Test>(key));
+ // }
+
+
+ // [Test]
+ // [TestCase(typeof(CacheTable<int>))]
+ // [TestCase(typeof(MemoryCache<int>))]
+ // public void Can_Turn_Cache_On(Type type)
+ // {
+ // var baseKey = "Can_Turn_Cache_Off";
+
+ // var objectCache = (IAbstractObjectCache)System.Activator.CreateInstance(type, new object[] { baseKey});
+ // objectCache.TurnCacheOff();
+
+ // var templateString = "templateString";
+ // var key = "Type type";
+
+ // var test = new Test();
+ // test.MyProperty = "Can_Turn_Cache_Off";
+
+ // Assert.IsFalse(objectCache.UseCache);
+
+ // objectCache.Add<Test>(key, test, templateString, ObjectCachePriority.High);
+
+ // Assert.IsNull(objectCache.Get<Test>(key));
+
+ // objectCache.TurnCacheOn();
+
+ // Assert.IsTrue(objectCache.UseCache);
+
+ // objectCache.Add<Test>(key, test, templateString, ObjectCachePriority.High);
+
+ // Assert.AreEqual(test, objectCache.Get<Test>(key));
+ // }
+
+
+ // [Test]
+ // public void ObjectCachePriority_Medium_Returned_When_Parse_Invalid_String_To_ObjectCachePriority(Type type)
+ // {
+ // Assert.AreEqual(ObjectCachePriority.Medium, ObjectCache.ParseObjectCachePriority("Null_Returned_When_Parse_Invalid_String_To_ObjectCachePriority"));
+ // }
+
+ #region Stubs
+
+ public class StubClass
+ {
+
+ public string MyProperty { get; set; }
+ }
+
+ public interface IStubInterface
+ {
+
+ }
+
+
+
+
+ #endregion
+
+ }
+}
View
6 Tests/Unit Tests/Glass.Mapper.Tests/Caching/CacheKeyResolverFixture.cs
@@ -25,17 +25,19 @@ public void ResolveKey_GetKey()
Context.ResolverFactory = Substitute.For<IDependencyResolverFactory>();
Context.ResolverFactory.GetResolver().Returns(Substitute.For<IDependencyResolver>());
Context context = Context.Create(glassConfig);
-
+
AbstractTypeCreationContext abstractTypeCreationContext = Substitute.For<AbstractTypeCreationContext>();
abstractTypeCreationContext.RequestedType.Returns(type);
var cacheConfiguration = Substitute.For<AbstractObjectCacheConfiguration>();
cacheConfiguration.ObjectCache = Substitute.For<AbstractObjectCache<int>>();
+ context.ObjectCacheConfiguration.Returns(cacheConfiguration);
+
var configuration = Substitute.For<AbstractTypeConfiguration>();
configuration.Type = type;
- var args = new ObjectConstructionArgs(context, abstractTypeCreationContext, configuration, service, cacheConfiguration);
+ var args = new ObjectConstructionArgs(context, abstractTypeCreationContext, configuration, service);
var key = Substitute.For<CacheKey<int>>();
View
7 Tests/Unit Tests/Glass.Mapper.Tests/Caching/CacheTableFixture.cs
@@ -22,7 +22,7 @@ public void SetUp()
var args = Substitute.For<ObjectConstructionArgs>();
var cacheKeyResolver = Substitute.For<AbstractCacheKeyResolver<int>>();
_cacheKey = Substitute.For<CacheKey<int>>();
- cacheKeyResolver.GetKey(args).Returns(_cacheKey);
+ cacheKeyResolver.GetKey(args).ReturnsForAnyArgs(_cacheKey);
_cacheTable = new CacheTable<int>(cacheKeyResolver);
_cacheTable.ClearCache();
}
@@ -56,7 +56,6 @@ public void CanGetContansObjectFromCacheTable()
var args = Substitute.For<ObjectConstructionArgs>();
args.Result.Returns(stubClass);
-
//Act
_cacheTable.AddObject(args);
@@ -78,15 +77,13 @@ public void CantGetContansObjectFromCacheTable()
//Act
- Assert.IsFalse(_cacheTable.AddObject(args));
+ _cacheTable.AddObject(args);
//Assert
Assert.AreEqual(args.Result, null);
Assert.IsFalse(_cacheTable.ContansObject(args));
-
}
-
#region Stubs
View
1  Tests/Unit Tests/Glass.Mapper.Tests/Glass.Mapper.Tests.csproj
@@ -68,6 +68,7 @@
<ItemGroup>
<Compile Include="AbstractServiceFixture.cs" />
<Compile Include="Caching\CacheTableFixture.cs" />
+ <Compile Include="Caching\AbstractObjectCacheFixture.cs" />
<Compile Include="Configuration\AbstractTypeConfigurationFixture.cs" />
<Compile Include="Configuration\Attributes\AttributeConfigurationLoaderFixture.cs" />
<Compile Include="Configuration\Attributes\AbstractClassAttributeFixture.cs" />
View
8 ...apper.Tests/Pipelines/ObjectConstruction/Tasks/ObjectCachingResolver/ObjectCachingResolverTaskFixture.cs
@@ -36,13 +36,15 @@ public void Execute_ObjectCachingResolver_ObjectReturned()
AbstractObjectCacheConfiguration cacheConfiguration = Substitute.For<AbstractObjectCacheConfiguration>();
cacheConfiguration.ObjectCache = Substitute.For<AbstractObjectCache<int>>();
+ context.ObjectCacheConfiguration.Returns(cacheConfiguration);
+
AbstractTypeCreationContext abstractTypeCreationContext = Substitute.For<AbstractTypeCreationContext>();
abstractTypeCreationContext.RequestedType.Returns(type);
var configuration = Substitute.For<AbstractTypeConfiguration>();
configuration.Type = type;
- ObjectConstructionArgs args = new ObjectConstructionArgs(context, abstractTypeCreationContext, configuration, service, cacheConfiguration);
+ ObjectConstructionArgs args = new ObjectConstructionArgs(context, abstractTypeCreationContext, configuration, service);
cacheConfiguration.ObjectCache.GetObject(args).ReturnsForAnyArgs(new StubClass());
@@ -75,10 +77,12 @@ public void Execute_ObjectCachingResolver_NotObjectReturned()
AbstractObjectCacheConfiguration cacheConfiguration = Substitute.For<AbstractObjectCacheConfiguration>();
cacheConfiguration.ObjectCache = Substitute.For<AbstractObjectCache<int>>();
+ context.ObjectCacheConfiguration.Returns(cacheConfiguration);
+
var configuration = Substitute.For<AbstractTypeConfiguration>();
configuration.Type = type;
- ObjectConstructionArgs args = new ObjectConstructionArgs(context, abstractTypeCreationContext, configuration, service, cacheConfiguration);
+ ObjectConstructionArgs args = new ObjectConstructionArgs(context, abstractTypeCreationContext, configuration, service);
cacheConfiguration.ObjectCache.ContansObject(args).ReturnsForAnyArgs(false);
View
4 ...lass.Mapper.Tests/Pipelines/ObjectConstruction/Tasks/ObjectCachingSaver/ObjectCachingSaverTaskFixture.cs
@@ -42,13 +42,15 @@ public void Execute_ObjectCachingResolver_ObjectReturned()
cacheConfiguration.ObjectCache = Substitute.For<AbstractObjectCache<int>>(cacheKeyResolver);
+ context.ObjectCacheConfiguration.Returns(cacheConfiguration);
+
AbstractTypeCreationContext abstractTypeCreationContext = Substitute.For<AbstractTypeCreationContext>();
abstractTypeCreationContext.RequestedType.Returns(type);
var configuration = Substitute.For<AbstractTypeConfiguration>();
configuration.Type = type;
- var args = new ObjectConstructionArgs(context, abstractTypeCreationContext, configuration, service, cacheConfiguration);
+ var args = new ObjectConstructionArgs(context, abstractTypeCreationContext, configuration, service);
args.Result = new StubClass();
cacheKeyResolver.GetKey(args).ReturnsForAnyArgs(Substitute.For<CacheKey<int>>());
Please sign in to comment.
Something went wrong with that request. Please try again.