P)Can you show a simple code showing file dependency in cache

Q

P)Can you show a simple code showing file dependency in cache ?

✍: Guest

A
Partial Class Default_aspx
Public Sub displayAnnouncement()
Dim announcement As String
If Cache(“announcement”) Is Nothing Then
Dim file As New _
System.IO.StreamReader _
(Server.MapPath(“announcement.txt”))
announcement = file.ReadToEnd
file.Close()
Dim depends As New _
System.Web.Caching.CacheDependency _
(Server.MapPath(“announcement.txt”))
Cache.Insert(“announcement”, announcement, depends)
End If
Response.Write(CType(Cache(“announcement”), String))
End Sub
Private Sub Page_Init(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Init
displayAnnouncement()
End Sub
End Class

Above given method displayAnnouncement() displays banner text from Announcement.txt file which is lying in application path of the web directory. Above method first checks whether the Cache object is nothing, if the cache object is nothing then it moves further to load the cache data from the file. Whenever the file data changes the cache object is removed and set to nothing.

2007-10-23, 4669👍, 0💬