Permalink
Browse files

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

…t in the construction args
  • Loading branch information...
1 parent ff027a3 commit e452928483f0eb1f4c27407f3a3b26201bcc49a8 @gravypower gravypower committed Jan 10, 2013
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 ...lass.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 ...ests/Pipelines/ObjectConstruction/Tasks/ObjectCachingResolver/ObjectCachingResolverTaskFixture.cs
  28. +3 −1 ...pper.Tests/Pipelines/ObjectConstruction/Tasks/ObjectCachingSaver/ObjectCachingSaverTaskFixture.cs
@@ -17,9 +17,4 @@
<UseBuildPlatform></UseBuildPlatform>
<ProxyProcessPath></ProxyProcessPath>
<UseCPUArchitecture>AutoDetect</UseCPUArchitecture>
- <IgnoredTests>
- <RegexTestSelector>
- <RegularExpression>.*</RegularExpression>
- </RegexTestSelector>
- </IgnoredTests>
</ProjectConfiguration>
@@ -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;
}
}
}
@@ -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
{
@@ -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))
@@ -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);
@@ -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;
+ }
+ }
+}
@@ -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();
}
}
@@ -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
{
@@ -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();
+ }
+ }
+}
@@ -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;
- }
- }
-}
@@ -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);
}
@@ -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)
{
@@ -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>();
}
}
}
Oops, something went wrong.

0 comments on commit e452928

Please sign in to comment.