London Dev Meetup Rescheduled! Due to unavoidable reasons, the event has been moved to 21st May. Speakers remain the same—any changes will be communicated. Seats are limited—register here to secure your spot!

Creating and using a own CacheDependency in EPiServer

Vote:
 
After reading this thread, http://www.episerver.com/en/EPiServer_Knowledge_Center/Developer-Forum2/EPiServer-Developer-Forums-/1805/6409/, I have started using this cache metod after doing FindPagesWithCriteria searches, and it works very nice. In a project I am working on now we are using a third party plug in to EPiServer called ImageVault(http://www.meridium.se/templates/ProductPage____15.aspx?epslanguage=SV). In some PageTemplates I use a property called an ImageVaultAlbum property. With this i can render out many pictures stored in one album. For each picture in the ImageVault album i create a Image object and insert it into a System.Collection.Generic.List with images, and then render them out with help of a repeater. After the list is filled with Image objects, i thought about caching the list. If there has been no changes in ImageVault, I do not want the list to be generated again. So I then thought of making a CacheDependency based on DateTime.Now.Ticks and set this when changes has been made in ImageVault(by listening to the IVDataFactory.AfterAddFileEvent, IVDataFactory.AfterUpdateFileEvent, IVDataFactory.UpdateAlbumEvent, and IVDataFactory.DeleteFileEvent). The events I listen to fire when they are suppose to and the DateTime.Now.Ticks is updated. But when I insert the list of images in the cache, it is not working. There is no errormessage and when i insert the list in cache using DateTime absoluteExpiration and TimeSpan slidingExpiration, it works fine. So therfore I concluded with that my CacheDependency does not work. Does any one have a clue why this is not working? My code is listed at the end. Is it possible for me to create a VersionKey or a CacheDependency with the EPiServer.DataFactoryCache class? BR, Tore public const string IVCacheVersionKey = "Moelven.Global.IVCacheVersion"; public static long IVCacheVersion; protected void Application_Start(Object sender, EventArgs e) { // Init IVCacheVersionKey SetIVCacheVersion(null, null); // Update ImageVault IVCacheVersionKey IVDataFactory.AfterAddFileEvent += SetIVCacheVersion; IVDataFactory.AfterUpdateFileEvent += SetIVCacheVersion; IVDataFactory.UpdateAlbumEvent += SetIVCacheVersion; IVDataFactory.DeleteFileEvent += SetIVCacheVersion; } protected static void SetIVCacheVersion(object source, ImageStoreNET.CMS.Data.IVDataEventArgs e) { IVCacheVersion = DateTime.Now.Ticks; } // After the List of images is created string[] dependencyKey = new string[] {Global.IVCacheVersionKey}; CacheDependency dependency = new CacheDependency(null, dependencyKey); HttpContext.Current.Cache.Insert(key, imageList, dependency);
#16019
Mar 03, 2008 21:43
Vote:
 
Can you paste the code where you're inserting an updating the cache item you're adding an dependency to? Or - could this be the error? The dependency should be against another cache item. So, your SetIVCacheVersion method should update the value of an entry in the cache. /Steve
#16823
Mar 08, 2008 13:23
Vote:
 
Hi Steve. My problem was that I did not insert and update the cache item. So now it works perfectly. Thanks for leading my on to the right track :) Is there a reason why the EPiServer.StaticCacheKey is inaccessible for? Had to make my own IVCacheKey that does the same as StaticCacheKey. private static IVCacheKey _versionKey; public const string IVCacheVersion = "Moelven.Global.IVCacheVersion"; protected void Application_Start(Object sender, EventArgs e) { // Init _versionKey _versionKey = new IVCacheKey("Moelven.Global.IVCacheVersion"); SetIVCacheVersion(null, null); // Update ImageVault _versionKey IVDataFactory.AfterAddFileEvent += SetIVCacheVersion; IVDataFactory.AfterUpdateFileEvent += SetIVCacheVersion; IVDataFactory.UpdateAlbumEvent += SetIVCacheVersion; IVDataFactory.DeleteFileEvent += SetIVCacheVersion; } protected static void SetIVCacheVersion(object source, ImageStoreNET.CMS.Data.IVDataEventArgs e) { _versionKey.Value = DateTime.Now.Ticks; } // Properties public static long Version { get { return _versionKey.Value; } } //Internal class internal class IVCacheKey { // Fields private readonly string _keyName; // Methods internal IVCacheKey(string keyName) { _keyName = keyName; EnsureKey(); } internal void EnsureKey() { if (CacheManager.RuntimeCacheGet(_keyName) == null) { SetKey(); } } private void SetKey() { SetKey(DateTime.Now.Ticks); } private void SetKey(long value) { CacheManager.RuntimeCacheInsert(_keyName, value, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable); } internal void Update() { CacheManager.RuntimeCacheInsert(_keyName, DateTime.Now.Ticks, null, DateTime.MaxValue, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable); UpdateRemoteOnly(); } internal void UpdateRemoteOnly() { CacheManager.RemoveRemoteOnly(_keyName); } // Properties internal string Name { get { return _keyName; } } internal long Value { get { EnsureKey(); return (long)CacheManager.RuntimeCacheGet(_keyName); } set { SetKey(value); } } } // After the List of images is created string[] dependencyKey = new string[] { Global.IVCacheVersion }; CacheDependency dependency = new CacheDependency(null, dependencyKey); HttpContext.Current.Cache.Insert(key, imageList, dependency);
#16824
Mar 11, 2008 11:07
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.