Permalink
Browse files

some cahnges for caching

  • Loading branch information...
1 parent 028cdf0 commit 4d39fc9f30300f9de33c0100c5b61123f91ad23b @gravypower gravypower committed Jan 18, 2013
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
+using Glass.Mapper.Caching.Proxy;
using Sitecore.Data.Managers;
using Sitecore.Data;
using Sitecore;
@@ -63,19 +64,20 @@ private void CacheWorker(Database database)
if (entries.Count > 0)
{
var relatedKeyList = new List<string>();
+
var cache = Context.Default.ObjectCacheConfiguration.ObjectCache;
foreach (var entry in entries)
{
//get the item action log string
var logString = String.Format("{0}-{1}", entry.ItemId, entry.Action);
- //we only want to process save and delete events
- //updates are also processed as a save event
- if (!performedActions.Contains(logString) && (entry.Action == HistoryAction.Deleted || entry.Action == HistoryAction.Saved))
- {
- performedActions.Add(logString);
- }
+ ////we only want to process save and delete events
+ ////updates are also processed as a save event
+ //if (!performedActions.Contains(logString) && (entry.Action == HistoryAction.Deleted || entry.Action == HistoryAction.Saved))
+ //{
+ // performedActions.Add(logString);
+ //}
var item = database.GetItem(entry.ItemId);
@@ -99,6 +101,7 @@ private void CacheWorker(Database database)
//update the last update time
database.Properties[LastUpdate + Environment.MachineName] = DateUtil.ToIsoDate(endTime, true);
+ CacheMethodInterceptor.LastUpdated = DateTime.Now;
}
private static DateTime LastUpdateTime(Database database)
@@ -142,6 +142,7 @@ 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()
@@ -2,7 +2,7 @@
using Castle.DynamicProxy;
using Glass.Mapper.Proxies;
-namespace Glass.Mapper.ObjectCaching.Proxy
+namespace Glass.Mapper.Caching.Proxy
{
public class CacheInterfaceMethodInterceptor : IInterceptor
{
@@ -41,7 +41,6 @@ public void Intercept(IInvocation invocation)
Context.Default.ObjectCacheConfiguration.ObjectCache.GetLatestCacheKey(_cacheKey.GetId());
if (!newCacheKey.Equals(_cacheKey))
{
-
_originalTarget = Context.Default.ObjectCacheConfiguration.ObjectCache.GetObject(newCacheKey);
_values = new Dictionary<string, object>();
_cacheKey = newCacheKey;
@@ -1,7 +1,10 @@
using System;
+using System.Collections.Generic;
using System.Linq;
+using System.Reflection;
+using System.Reflection.Emit;
using Castle.DynamicProxy;
-using Glass.Mapper.ObjectCaching.Proxy;
+using Glass.Mapper.Caching.Proxy;
using Glass.Mapper.Pipelines.ObjectConstruction;
using Glass.Mapper.Proxies;
@@ -11,7 +14,9 @@ namespace Glass.Mapper.Caching.Proxy
public class CacheProxyGenerator
{
private static readonly ProxyGenerator Generator = new ProxyGenerator();
- private static readonly ProxyGenerationOptions Options = new ProxyGenerationOptions(new CacheProxyGeneratorHook());
+
+ private static readonly ProxyGenerationOptions Options =
+ new ProxyGenerationOptions(new CacheProxyGeneratorHook());
public static object CreateProxy(object originalTarget, ObjectConstructionArgs args)
@@ -25,16 +30,93 @@ public static object CreateProxy(object originalTarget, ObjectConstructionArgs a
var interceptors = oldProxy.GetInterceptors();
if (interceptors.Any(x => x is InterfaceMethodInterceptor))
{
- var subInterceptor = interceptors.First(x => x is InterfaceMethodInterceptor).CastTo<InterfaceMethodInterceptor>();
+ var subInterceptor =
+ interceptors.First(x => x is InterfaceMethodInterceptor).CastTo<InterfaceMethodInterceptor>();
return Generator.CreateInterfaceProxyWithoutTarget(
type,
new CacheInterfaceMethodInterceptor(subInterceptor));
-
+
}
return oldProxy;
}
return Generator.CreateClassProxy(type, Options, new CacheMethodInterceptor(originalTarget, args));
}
+
+
+
+ //public static object CreateInstance(Type vmType)
+ //{
+ // VerifyViewModelType(vmType);
+
+ // // Create everything required to get a module builder
+ // AssemblyName assemblyName = new AssemblyName("SmartViewModelDynamicAssembly");
+ // AppDomain domain = AppDomain.CurrentDomain;
+ // AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
+ // //AssemblyBuilderAccess.RunAndSave);
+ // ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name);
+
+ // string dynamicTypeName = Assembly.CreateQualifiedName(vmType.AssemblyQualifiedName, "Smart" + vmType.Name);
+
+ // TypeBuilder typeBuilder = moduleBuilder.DefineType(dynamicTypeName,
+ // TypeAttributes.Public | TypeAttributes.Class, vmType);
+
+ // MethodInfo raisePropertyChangedMethod = vmType.GetMethod("RaisePropertyChanged",
+ // BindingFlags.NonPublic |
+ // BindingFlags.Instance, null,
+ // new Type[] {typeof (string)}, null);
+
+ // foreach (PropertyInfo propertyInfo in FindNotifyPropertyChangCandidates(vmType))
+ // UpdateProperty(propertyInfo, typeBuilder, raisePropertyChangedMethod);
+
+ // Type dynamicType = typeBuilder.CreateType();
+
+ // return (T) Activator.CreateInstance(dynamicType);
+ //}
+
+ //private static void VerifyViewModelType(Type vmType)
+ //{
+ // if (vmType.IsSealed)
+ // throw new InvalidOperationException("The specified view model type is not allowed to be sealed.");
+ //}
+
+ //private static IEnumerable<PropertyInfo> FindNotifyPropertyChangCandidates(Type vmType)
+ //{
+ // return from p in vmType.GetProperties()
+ // where p.GetSetMethod() != null && p.GetSetMethod().IsVirtual &&
+ // p.GetCustomAttributes(typeof (RaisePropertyChangedAttribute), false).Length > 0
+ // select p;
+ //}
+
+ //private static void UpdateProperty(PropertyInfo propertyInfo, TypeBuilder typeBuilder,
+ // MethodInfo raisePropertyChangedMethod)
+ //{
+ // // Update the setter of the class
+ // PropertyBuilder propertyBuilder = typeBuilder.DefineProperty(propertyInfo.Name,
+ // PropertyAttributes.None,
+ // propertyInfo.PropertyType, null);
+
+ // // Create set method
+ // MethodBuilder builder = typeBuilder.DefineMethod("set_" + propertyInfo.Name,
+ // MethodAttributes.Public | MethodAttributes.Virtual, null,
+ // new Type[] {propertyInfo.PropertyType});
+ // builder.DefineParameter(1, ParameterAttributes.None, "value");
+ // ILGenerator generator = builder.GetILGenerator();
+
+ // // Add IL code for set method
+ // generator.Emit(OpCodes.Nop);
+ // generator.Emit(OpCodes.Ldarg_0);
+ // generator.Emit(OpCodes.Ldarg_1);
+ // generator.Emit(OpCodes.Call, propertyInfo.GetSetMethod());
+
+ // // Call property changed for object
+ // generator.Emit(OpCodes.Nop);
+ // generator.Emit(OpCodes.Ldarg_0);
+ // generator.Emit(OpCodes.Ldstr, propertyInfo.Name);
+ // generator.Emit(OpCodes.Callvirt, raisePropertyChangedMethod);
+ // generator.Emit(OpCodes.Nop);
+ // generator.Emit(OpCodes.Ret);
+ // propertyBuilder.SetSetMethod(builder);
+ //}
}
}
@@ -79,6 +79,7 @@
<Compile Include="Caching\ObjectCaching\Exceptions\KeyNullOrEmptyObjectCacheException.cs" />
<Compile Include="Caching\ObjectCaching\ReaderWriterLock.cs" />
<Compile Include="Caching\ObjectCaching\Exceptions\ReleatedTemplateIDNullOrEmptyObjectCacheException.cs" />
+ <Compile Include="Caching\Proxy\CacheInterfaceMethodInterceptor.cs" />
<Compile Include="Caching\Proxy\CacheLazyObjectInterceptor.cs" />
<Compile Include="Caching\Proxy\CacheMethodInterceptor.cs" />
<Compile Include="Caching\Proxy\CacheProxyGenerator.cs" />
@@ -117,7 +118,6 @@
<Compile Include="IDependencyResolverFactory.cs" />
<Compile Include="IDependencyResolver.cs" />
<Compile Include="IGlassConfiguration.cs" />
- <Compile Include="ObjectCaching\Proxy\CacheInterfaceMethodInterceptor.cs" />
<Compile Include="Profilers\IPerformanceProfiler.cs" />
<Compile Include="LazyEnumerable.cs" />
<Compile Include="MapperException.cs" />
@@ -16,8 +16,10 @@ public override void Execute(ObjectConstructionArgs args)
if (args.Context.ObjectCacheConfiguration == null) return;
if (!args.Context.ObjectCacheConfiguration.ObjectCache.ContansObject(args)) return;
-
+ //args.Result = args.Context.ObjectCacheConfiguration.ObjectCache.GetObject(args);
args.Result = CacheProxyGenerator.CreateProxy(args.Context.ObjectCacheConfiguration.ObjectCache.GetObject(args), args);
+
+
args.ObjectOrigin = ObjectOrigin.ObjectCachingResolver;
args.AbortPipeline();
}
@@ -13,8 +13,10 @@ public override void Execute(ObjectConstructionArgs args)
{
if (CacheDisabler.CacheDisabled) return;
+ if (args.Context.ObjectCacheConfiguration == null) return;
+
//Save item to the cache
- args.Context.ObjectCacheConfiguration.ObjectCache.AddObject(args);
+ //args.Context.ObjectCacheConfiguration.ObjectCache.AddObject(args);
args.Result = CacheProxyGenerator.CreateProxy(args.Result, args);
}
}
@@ -3,6 +3,7 @@
using System.Diagnostics;
using System.Linq;
using System.Text;
+using Glass.Mapper.Caching;
using Glass.Mapper.Profilers;
using Glass.Mapper.Sc.Configuration;
using Glass.Mapper.Sc.Configuration.Attributes;
@@ -67,7 +68,7 @@ public void Setup()
[Test]
[Timeout(120000)]
[Repeat(10000)]
- public void GetItems(
+ public void GetItem(
[Values(1000, 10000, 50000)] int count
)
{
@@ -95,6 +96,39 @@ public void Setup()
Console.WriteLine("Preformance Test Count: {0} Ratio: {1}".Formatted(count, total));
}
+ [Test]
+ [Timeout(120000)]
+ [Repeat(10000)]
+ public void GetItemsNoCache(
+ [Values(1000, 10000, 50000)] int count
+ )
+ {
+ using (new CacheDisabler())
+ {
+ for (int i = 0; i < count; i++)
+ {
+ _glassWatch.Reset();
+ _rawWatch.Reset();
+
+ _rawWatch.Start();
+ var rawItem = _db.GetItem(new ID(_id));
+ var value1 = rawItem["Field"];
+ _rawWatch.Stop();
+ _rawTotal = _rawWatch.ElapsedTicks;
+
+ _glassWatch.Start();
+ var glassItem = _service.GetItem<StubClass>(_id);
+ var value2 = glassItem.Field;
+ _glassWatch.Stop();
+ _glassTotal = _glassWatch.ElapsedTicks;
+
+ }
+
+ double total = _glassTotal/_rawTotal;
+ Console.WriteLine("Preformance Test Count: {0} Ratio: {1}".Formatted(count, total));
+ }
+ }
+
#region Stubs
[SitecoreType]
@@ -103,6 +137,9 @@ public class StubClass
[SitecoreField(Setting = SitecoreFieldSettings.RichTextRaw)]
public virtual string Field { get; set; }
+ [SitecoreField("Field")]
+ public virtual string Field1 { get; set; }
+
[SitecoreId]
public virtual Guid Id { get; set; }
}

0 comments on commit 4d39fc9

Please sign in to comment.