Skip to content

Commit

Permalink
Working on caching proxy updater
Browse files Browse the repository at this point in the history
updated the cache disabler
  • Loading branch information
gravypower committed Jan 17, 2013
1 parent 9e1e2bb commit cd0d841
Show file tree
Hide file tree
Showing 29 changed files with 501 additions and 209 deletions.
Expand Up @@ -17,4 +17,9 @@
<UseBuildPlatform></UseBuildPlatform>
<ProxyProcessPath></ProxyProcessPath>
<UseCPUArchitecture>AutoDetect</UseCPUArchitecture>
<IgnoredTests>
<RegexTestSelector>
<RegularExpression>.*</RegularExpression>
</RegexTestSelector>
</IgnoredTests>
</ProjectConfiguration>
6 changes: 0 additions & 6 deletions Sites/Sitecore/Glass.Mapper.Sites.Sc/layouts/TestSub.ascx.cs
@@ -1,12 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Glass.Mapper.Sc;
using Glass.Mapper.Sc.Configuration.Attributes;
using Glass.Mapper.Sc.Integration;
using Glass.Mapper.Sites.Sc.Models;

namespace Glass.Mapper.Sites.Sc.layouts
Expand Down
2 changes: 0 additions & 2 deletions Source/Glass.Mapper.Sc/GlassConfig.cs
Expand Up @@ -142,8 +142,6 @@ public override void Configure(WindsorContainer container, string contextName)

Component.For<IObjectSavingTask>().ImplementedBy<StandardSavingTask>().LifestyleTransient(),



Component.For<AbstractObjectCacheConfiguration>().ImplementedBy<ObjectCacheConfiguration>().LifestyleTransient(),
Component.For<IAbstractObjectCache>().ImplementedBy<CacheTable<Guid>>().LifestyleTransient(),
Component.For<AbstractCacheKeyResolver<Guid>>().ImplementedBy<SitecoreCacheKeyResolver>().LifestyleTransient()
Expand Down
55 changes: 10 additions & 45 deletions Source/Glass.Mapper/AbstractService.cs
Expand Up @@ -16,18 +16,8 @@ namespace Glass.Mapper
{
public abstract class AbstractService : IAbstractService
{

private IPerformanceProfiler _profiler;

/// <summary>
/// The list of tasks to be performed by the Object Construction Pipeline. Called in the order specified.
/// </summary>
private IEnumerable<IObjectConstructionTask> ObjectConstructionTasks { get; set; }

/// <summary>
/// The list of tasks to be performed by the Object Construction Pipeline. Called in the order specified.
/// </summary>
private IEnumerable<ObjectConstructionTask> ObjectConstructionTasks { get; set; }
public IPerformanceProfiler Profiler
{
get { return _profiler; }
Expand All @@ -41,9 +31,7 @@ public IPerformanceProfiler Profiler
}
}

public Context GlassContext { get; private set; }


public Context GlassContext { get; private set; }

private TypeResolver _typeResolver;

Expand All @@ -52,7 +40,7 @@ public IPerformanceProfiler Profiler
private ObjectConstruction _objectConstruction;

private ObjectSaving _objectSaving;

public AbstractService()
: this(Context.Default)
{
Expand All @@ -69,17 +57,11 @@ public AbstractService(Context glassContext)


GlassContext = glassContext;
if (GlassContext == null)
if (GlassContext == null)
throw new NullReferenceException("Context is null");


ObjectConstructionTasks = glassContext.DependencyResolver.ResolveAllInOrder<ObjectConstructionTask>("Order");
TypeResolverTasks = glassContext.DependencyResolver.ResolveAll<ITypeResolverTask>();
ConfigurationResolverTasks = glassContext.DependencyResolver.ResolveAll<IConfigurationResolverTask>();
ObjectSavingTasks = glassContext.DependencyResolver.ResolveAll<IObjectSavingTask>();

var objectConstructionTasks = glassContext.DependencyResolver.ResolveAll<IObjectConstructionTask>();
_objectConstruction = new ObjectConstruction(objectConstructionTasks);
var objectConstructionTasks = glassContext.DependencyResolver.ResolveAllInOrder<ObjectConstructionTask>("Order");
_objectConstruction = new ObjectConstruction(objectConstructionTasks);

var typeResolverTasks = glassContext.DependencyResolver.ResolveAll<ITypeResolverTask>();
_typeResolver = new TypeResolver(typeResolverTasks);
Expand Down Expand Up @@ -107,28 +89,17 @@ public object InstantiateObject(AbstractTypeCreationContext abstractTypeCreation
//run the pipeline to get the configuration to load
var configurationArgs = new ConfigurationResolverArgs(GlassContext, abstractTypeCreationContext, typeArgs.Result);
_configurationResolver.Run(configurationArgs);

if (configurationArgs.Result == null)
throw new NullReferenceException("Configuration Resolver pipeline did not return type.");

var config = configurationArgs.Result;

//Run the object construction
_objectConstruction.Run(objectArgs);
var objectArgs = new ObjectConstructionArgs(GlassContext, abstractTypeCreationContext, config, this);

var objectArgs = new ObjectConstructionArgs(GlassContext, abstractTypeCreationContext,
config, this);
if (DisableCache)
{
using (new CacheDisabler(objectArgs))
{
objectRunner.Run(objectArgs);
}
}
else
{
objectRunner.Run(objectArgs);
}

_objectConstruction.Run(objectArgs);


return objectArgs.Result;
Expand Down Expand Up @@ -157,17 +128,13 @@ public void SaveObject(AbstractTypeSavingContext abstractTypeSavingContext)
/// <param name="obj"></param>
/// <returns></returns>
public abstract AbstractDataMappingContext CreateDataMappingContext(AbstractTypeSavingContext creationContext);


public bool DisableCache { get; set; }
}

public interface IAbstractService
{
Context GlassContext { get; }
Context GlassContext { get; }

object InstantiateObject(AbstractTypeCreationContext abstractTypeCreationContext);

/// <summary>
/// Used to create the context used by DataMappers to map data to a class
/// </summary>
Expand All @@ -183,7 +150,5 @@ public interface IAbstractService
/// <param name="creationContext">The Saving Context</param>
/// <returns></returns>
AbstractDataMappingContext CreateDataMappingContext(AbstractTypeSavingContext creationContext);

bool DisableCache { get; set; }
}
}
2 changes: 0 additions & 2 deletions Source/Glass.Mapper/AbstractTypeCreationContext.cs
Expand Up @@ -12,8 +12,6 @@ namespace Glass.Mapper
/// </summary>
public abstract class AbstractTypeCreationContext
{


public bool InferType { get; set; }
public bool IsLazy { get; set; }
public Type RequestedType { get; set; }
Expand Down
60 changes: 47 additions & 13 deletions Source/Glass.Mapper/Caching/CacheDisabler.cs
@@ -1,32 +1,66 @@
using System;
using System.Web;
using Glass.Mapper.Pipelines.ObjectConstruction;

namespace Glass.Mapper.Caching
{
public class CacheDisabler:IDisposable
{
private readonly ObjectConstructionArgs _args;
private readonly IAbstractService _abstractService;

public CacheDisabler(ObjectConstructionArgs args)
public static bool CacheDisabled
{
this._args = args;
args.DisableCache = true;
get
{
return HttpContext.Current != null ? ReturnFromHttpContxt() : ReturnFromThreadStatic();
}
set
{
if (HttpContext.Current != null)
{
SetToHttpContext(value);
}

SetToThreadStatic(value);
}
}

public CacheDisabler(IAbstractService abstractService)
private static void SetToHttpContext(bool value)
{
this._abstractService = abstractService;
_abstractService.DisableCache = true;
HttpContext.Current.Items["Glass_CacheDisabled"] = value;
}

public void Dispose()
private static bool ReturnFromHttpContxt()
{
if (HttpContext.Current.Items.Contains("Glass_CacheDisabled"))
{
{
return (bool)HttpContext.Current.Items["Glass_CacheDisabled"];
}
}
return false;
}

[ThreadStatic]
private static bool _threadStaticCacheDisabled;

private static void SetToThreadStatic(bool value)
{
if(_args != null)
_args.DisableCache = false;
_threadStaticCacheDisabled = value;
}

if(_abstractService != null)
_abstractService.DisableCache = false;
private static bool ReturnFromThreadStatic()
{
return _threadStaticCacheDisabled;
}

public CacheDisabler()
{
CacheDisabled = true;
}

public void Dispose()
{
CacheDisabled = false;
}
}
}
6 changes: 6 additions & 0 deletions Source/Glass.Mapper/Caching/CacheKeyResolving/CacheKey.cs
Expand Up @@ -28,5 +28,11 @@ public override string ToString()
}

public abstract bool Equals(CacheKey<TIdType> other);


public object GetId()
{
return Id;
}
}
}
1 change: 1 addition & 0 deletions Source/Glass.Mapper/Caching/CacheKeyResolving/ICacheKey.cs
Expand Up @@ -3,5 +3,6 @@
public interface ICacheKey
{
bool Equals(object other);
object GetId();
}
}
28 changes: 25 additions & 3 deletions Source/Glass.Mapper/Caching/ObjectCaching/AbstractObjectCache.cs
Expand Up @@ -3,16 +3,18 @@
using System.Threading;
using Glass.Mapper.Caching.CacheKeyResolving;
using Glass.Mapper.Caching.ObjectCaching.Exceptions;
using Glass.Mapper.Caching.Proxy;
using Glass.Mapper.Pipelines.ObjectConstruction;
using System.Linq;

namespace Glass.Mapper.Caching.ObjectCaching
{
public abstract class AbstractObjectCache<TIdType> : IAbstractObjectCache
{
private readonly CacheInformation _cacheInformation = new CacheInformation();
private readonly CacheInformation<TIdType> _cacheInformation = new CacheInformation<TIdType>();
public string BaseCacheKey { get; set; }



public string DefaultBaseCacheKey
{
get { return "GlassObjectCahe"; }
Expand All @@ -26,7 +28,6 @@ public string DefaultBaseCacheKey
protected abstract void InternalAddObject(string objectKey, object objectForCaching);
protected abstract bool InternalClearCache();


protected AbstractObjectCache(string baseCacheKey)
{
BaseCacheKey = baseCacheKey;
Expand Down Expand Up @@ -64,6 +65,21 @@ public void AddObject(ObjectConstructionArgs args)
InternalAddObject(CacheKeyResolver.GetKey(args).ToString(), args.Result);
}

public object GetObject(ICacheKey cacheKey)
{
return InternalGetObject(cacheKey.ToString());
}

public bool ContansObject(ICacheKey cacheKey)
{
return InternalContansObject(cacheKey.ToString());
}

public void AddObject(ICacheKey cacheKey, object objectForCaching)
{
InternalAddObject(cacheKey.ToString(), objectForCaching);
}

public bool ClearCache()
{
return InternalClearCache();
Expand Down Expand Up @@ -181,5 +197,11 @@ public bool RemoveFromRelatedCache(string releatedKey)

return InternalRemoveObject(releatedKey);
}


public ICacheKey GetLatestCacheKey(object id)
{
return _cacheInformation.GetCacheKeys((TIdType)id).Peek();
}
}
}

0 comments on commit cd0d841

Please sign in to comment.