hcurrie
10/28/2008 11:56:00 AM
I am wanting to use caching in a Windows Forms app. The help for the Cache
class says:
"The Cache class is not intended for use outside of ASP.NET applications. It
was designed and tested for use in ASP.NET to provide caching for Web
applications. In other types of applications, such as console applications
or Windows Forms applications, ASP.NET caching might not work correctly."
I have created a very simple test application, and so far it seems to work
fine, except that the expiration seems to take a little longer than the time
I specify.
Has anyone else tried this, and found any problems?
Public Class Form1
Private onRemove As CacheItemRemovedCallback
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
' set-up the delegate for the callback
onRemove = New CacheItemRemovedCallback(AddressOf RemovedCallback)
Debug.Print(Now)
HttpRuntime.Cache.Add("One", 1, Nothing, Cache.NoAbsoluteExpiration, New
TimeSpan(0, 0, 10), CacheItemPriority.Default, onRemove)
HttpRuntime.Cache.Add("Two", 2, Nothing, Cache.NoAbsoluteExpiration, New
TimeSpan(0, 0, 20), CacheItemPriority.Default, onRemove)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Debug.Print(Now)
MsgBox(HttpRuntime.Cache("One"))
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Debug.Print(Now)
MsgBox(HttpRuntime.Cache("Two"))
End Sub
Public Sub RemovedCallback(ByVal Key As String, ByVal Value As Object, ByVal
Reason As CacheItemRemovedReason)
Debug.Print(Now)
MsgBox(Key & "(" & Value & ") removed from the cache")
End Sub
End Class