You can download the full exaple as a zip file here.
In this example, I have also added a parameter in the event.
- The event:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.Serialization; namespace BOL.Events { [DataContract] public class ClearCacheEvent { public ClearCacheEvent(string cacheName) { this.CacheName = cacheName; } // Properties [DataMember] public string CacheName { get; protected set; } } }
- The event argument with the parameter:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sitecore.Events; namespace BOL.Events { [Serializable] public class ClearCacheEventArgs : EventArgs, IPassNativeEventArgs { public string CacheName { get; set; } public ClearCacheEventArgs(string cacheName) { this.CacheName = cacheName; } } }
- The event handler:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sitecore.Diagnostics; using Sitecore.Events; namespace BOL.Events { public class ClearCacheEventHandler { ///
/// This methos is used to raise the local event /// /// public static void Run(ClearCacheEvent @event) { Log.Info("ClearCacheEventHandler - Run", typeof(ClearCacheEventHandler)); ClearCacheEventArgs args = new ClearCacheEventArgs(@event.CacheName); Event.RaiseEvent("clearcache:remote", new object[] { args }); } } } - The hook to register the event:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sitecore.Events.Hooks; namespace BOL.Events { public class ClearCacheHook : IHook { public void Initialize() { Sitecore.Eventing.EventManager.Subscribe
(new Action { ClearCacheEventHandler.Run }); } } } - The code to trigger the event:
public class TriggerEvent { public void Trigger() { ClearCacheEvent inst = new ClearCacheEvent("Navigation"); Sitecore.Eventing.EventManager.QueueEvent
(inst); } } - The config file to register the event:
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> <sitecore> <hooks> <hook type="BOL.Events.ClearCacheHook, BOL"/> </hooks> <events> <event name="clearcache:remote"> <handler type="Shurgard.Events.ClearStoreCache, Shurgard.Events" method="OnRemoteClearStoreCache"/> </event> <events> </sitecore> </configuration>
Hi,
ReplyDeleteI have a requirement to clear the cache using sitecore remote events. Your blog was very helpful.
I am clearing the cache using 'HttpContext.Current.Cache.Remove("CacheKey") in the ClearCache Event Handler.
I am stuck on an issue. In the ClearCache event handler the HttpContext.Current object is null and hence while clearing the cache i get the argumentnull exception.
How do i got about it? Your help is appreciated.
Regards,
Sachin
It sound normal that the HttpContext.Current can bbe null in an event because it isn't an http request.
ReplyDeleteYou can try two things:
- Implement IRequiresSessionState (no method to implement but if should activate the httpcontext) in the event class but I am not sure at all that it will work.
- If it doesn't work you should consder to use Application to replace your cache key
I hope it will help you.
Benjamin