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

 Posted by ArticlesMaint on 9/17/2009 | Category: ASP.NET Interview questions | Views: 4360

Partial Class Default_aspx

Public Sub display Announcement()
Dim announcement As String

If Cache(“announcement”) Is Nothing Then
Dim file As New _
System.IO.StreamReader _
announcement = file.ReadToEnd
file. Close()
Dim depends As New _
System.Web.Caching.CacheDependency _
Cache.Insert(“announcement”, announcement, depends)
End If
Response.Write(CType(Cache(“announcement”), String))
End Sub

Private Sub Page_Init(ByVal sender As Object, By Val e As System.EventArgs) Handles Me. nit
display Announcement()
End Sub
End Class

Note: - Above source code can be obtained from CD in “CacheSample” folder.”Announcement.txt” is in the same folder which you can play around to see the results.

Above given method display Announcement() 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.

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response