Subscribe

RSS Feed (xml)

Implementing a CallBack when a Cached

The capability to call a method whenever an item expires from the cache allows you to


build systems that can intelligently add data back into the cache.


<Script runat=server>


Shared reason As CacheItemRemovedReason


Dim onRemove As CacheItemRemovedCallback


Public Shared Sub ItemRemovedCallback(key As String,


value As Object,


removedReason As CacheItemRemovedReason)


' Reason why the item was removed


reason = removedReason


' Reinsert the item into the Cache, but change the value


Cache.Insert(key, "Item re-inserted")


End Sub


Public Sub Page_Load(sender As Object, e As EventArgs)


onRemove = New CacheItemRemovedCallback(


AddressOf Me.ItemRemovedCallback)


' Add an item to the Cache with the callback


If Is Nothing(Cache("Key1")) Then


Cache.Insert("Key1", "In the Cache", Nothing,


DateTime.Now.AddSeconds(10),


TimeSpan.Zero, CacheItemPriority.Low, onRemove)


End If


' Set the label value


Label1.Text = Cache("Key1")


End Sub


</Script>


Value of the Cache: <asp:label id="Label1" runat="server" />


Comments


The capability of a function to be called when an item is removed from the cache is


very powerful. It allows you to reinsert the item or to report a reason as to why the item


was removed.


The only caveat is that during the time between when the item is removed from the


cache, yet before the callback function is called, another request might ask for the item


from the cache and would not find it. Regardless, it's still a very powerful technique for


controlling how items are removed from the cache.

No comments:

Archives

Variety in the Web World