There are a few ORM's for Siteocre and Glass is a great one. Like any framework though it has somethings that don't work they way you may need it to. For glass one of these may be how it generates a cache key. This article is focused on version 5 of glass.
Here is the code Glass uses for generating a cache key for all items it caches.You can see it uses a lot of the fields on a Sitecore item to generate the cache key. It does this to make sure it is caching a unique model. However, this is the cache key you are stuck with as there are no settings that will change this. Don't worry though, there is way to plug into Glass and tell it to use a different one.
The default cache key generator takes a lot of things into account, but if it does not take into account something you need can you still turn caching on for Glass models? The answer is yes! You just need to override this CacheKeyGenerator withour own implementation of ICacheKeyGenerator. This is a quick run down on how to do this.
First let's look at one more class and method. The ObjectConstructionTaskConfigFactory is where Glass sets up the cache key generator. First we need to notice the if statement. If caching always on is configured it sets up SitecoreCacheAlwaysOnCheckTask and if not it sets up SitecoreCacheCheckTask passing into each the CacheKeyGenerator. So if we want our on cache key generator we need to override these.
We are going to override the this in the GlassMapperScCustom.cs class.In the "CreateResolver" method we want to add the following code. We check to see if we are doing AlwaysOn caching or not and then remove the default object construction factory created in the code above with our own code. With this code we can now configured whatever cache key generator we want!
Now you are ready to move forward with your own cache key generation stratey inside Glass.
Here is the code Glass uses for generating a cache key for all items it caches.You can see it uses a lot of the fields on a Sitecore item to generate the cache key. It does this to make sure it is caching a unique model. However, this is the cache key you are stuck with as there are no settings that will change this. Don't worry though, there is way to plug into Glass and tell it to use a different one.
public class CacheKeyGenerator : ICacheKeyGenerator { public string Generate(ObjectConstructionArgs args) { SitecoreTypeCreationContext typeCreationContext = args.AbstractTypeCreationContext as SitecoreTypeCreationContext; return string.Format("{0}{1}{2}{3}{4}{5}{6}{7}", (object) typeCreationContext.SitecoreService.GlassContext.Name, Sitecore.Context.Site == null ? (object) string.Empty : (object) Sitecore.Context.Site.Name, (object) typeCreationContext.Item.ID, (object) typeCreationContext.Item["__Revision"], (object) typeCreationContext.Item.Language.Name, (object) typeCreationContext.Item.Database.Name, (object) typeCreationContext.Options.Type.FullName, (object) typeCreationContext.Options.Lazy); } }
The default cache key generator takes a lot of things into account, but if it does not take into account something you need can you still turn caching on for Glass models? The answer is yes! You just need to override this CacheKeyGenerator withour own implementation of ICacheKeyGenerator. This is a quick run down on how to do this.
First let's look at one more class and method. The ObjectConstructionTaskConfigFactory is where Glass sets up the cache key generator. First we need to notice the if statement. If caching always on is configured it sets up SitecoreCacheAlwaysOnCheckTask and if not it sets up SitecoreCacheCheckTask passing into each the CacheKeyGenerator. So if we want our on cache key generator we need to override these.
protected void Init() { Glass.Mapper.Config config = this.DependencyResolver.GetConfig(); this.Add<NullItemTask>((Func<NullItemTask>) (() => new NullItemTask())); this.Add<ModelDepthCheck>((Func<ModelDepthCheck>) (() => new ModelDepthCheck())); this.Add<ItemVersionCountByRevisionTask>((Func<ItemVersionCountByRevisionTask>) (() => new ItemVersionCountByRevisionTask())); this.Add<SitecoreItemTask>((Func<SitecoreItemTask>) (() => new SitecoreItemTask())); if (config.Cache.AlwaysOn) this.Add<SitecoreCacheAlwaysOnCheckTask>((Func<SitecoreCacheAlwaysOnCheckTask>) (() => new SitecoreCacheAlwaysOnCheckTask(this.DependencyResolver.CacheFactory, (ICacheKeyGenerator) new CacheKeyGenerator()))); else this.Add<SitecoreCacheCheckTask>((Func<SitecoreCacheCheckTask>) (() => new SitecoreCacheCheckTask(this.DependencyResolver.CacheFactory, (ICacheKeyGenerator) new CacheKeyGenerator()))); if (config.Debug.Enabled) this.Add<ConstructionTimerStart>((Func<ConstructionTimerStart>) (() => new ConstructionTimerStart((ICacheKeyGenerator) new CacheKeyGenerator(), this.DependencyResolver.GetLog(), this.DependencyResolver.GetConfig().Debug))); this.Add<EnforcedTemplateCheck>((Func<EnforcedTemplateCheck>) (() => new EnforcedTemplateCheck())); this.Add<CreateConcreteTask>((Func<CreateConcreteTask>) (() => new CreateConcreteTask(this.DependencyResolver.LazyLoadingHelper))); this.Add<CreateInterfaceTask>((Func<CreateInterfaceTask>) (() => new CreateInterfaceTask(this.DependencyResolver.LazyLoadingHelper))); }
We are going to override the this in the GlassMapperScCustom.cs class.In the "CreateResolver" method we want to add the following code. We check to see if we are doing AlwaysOn caching or not and then remove the default object construction factory created in the code above with our own code. With this code we can now configured whatever cache key generator we want!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public static IDependencyResolver CreateResolver() { var config = new Glass.Mapper.Sc.Config(); var dependencyResolver = new DependencyResolver(config); // Replace the standard GlassHtmlFactory with a custom factory, in order to override the default GlassHtml class. dependencyResolver.GlassHtmlFactory = new GlassHtmlFactory(dependencyResolver); if (config.Cache.AlwaysOn) { dependencyResolver.ObjectConstructionFactory.Remove<SitecoreCacheAlwaysOnCheckTask>(); dependencyResolver.ObjectConstructionFactory.InsertBefore<EnforcedTemplateCheck, SitecoreCacheAlwaysOnCheckTask>( (Func<SitecoreCacheAlwaysOnCheckTask>)(() => new SitecoreCacheAlwaysOnCheckTask(dependencyResolver.CacheFactory, (ICacheKeyGenerator)new MyCacheKeyGenerator()))); } else { dependencyResolver.ObjectConstructionFactory.Remove<SitecoreCacheCheckTask>(); dependencyResolver.ObjectConstructionFactory.InsertBefore<EnforcedTemplateCheck, SitecoreCacheCheckTask>( (Func<SitecoreCacheCheckTask>)(() => new SitecoreCacheCheckTask(dependencyResolver.CacheFactory, (ICacheKeyGenerator)new MyCacheKeyGenerator()))); } return dependencyResolver; } |
Now you are ready to move forward with your own cache key generation stratey inside Glass.
Comments
Harrah's 영주 출장샵 New 성남 출장마사지 Orleans Casino & Hotel - 777 Harrah's Boulevard 경기도 출장샵 New 남원 출장안마 Orleans, LA 70130-9646 (716) 755-7777 군포 출장마사지