D
darrel
In the past, I've used an XML to maintain a bunch of site data and cached
it. I'd only grab a new copy for the cache when the XML file was physically
updated:
-----------------------------------
Dim XMLfile As String
'check to see if the file exists in the cache
If System.Web.HttpContext.Current.Cache(siteID & "menuXML") Is Nothing Then
'read in the XML file
Dim file As New
System.IO.StreamReader(HttpContext.Current.Server.MapPath("/xml/" & siteID &
"/siteMenu.xml"))
XMLfile = file.ReadToEnd
file.Close()
' set the dependency (to only update when file is changed)
Dim depends As New
System.Web.Caching.CacheDependency(HttpContext.Current.Server.MapPath("/xml/"
& siteID & "/siteMenu.xml"))
'add the text to the cache object
System.Web.HttpContext.Current.Cache.Insert(siteID & "menuXML", XMLfile,
depends)
Else
'just use the cached version
XMLfile = CType(System.Web.HttpContext.Current.Cache(siteID & "menuXML"),
String)
End If
-----------------------------------
For a variety of reasons, we're no longer storing the XML as an XML file.
Instead, we're storing the XML in the database.
For now, that means on each page load, we're querying the DB and grabbing
the XML file. Not a huge deal, but it'd be nice to cache it. However, I
don't see a practical way to cache it and only update when the DB is
updated. It seems to me that to see if the DB is updated, I'd have to query
it anyways, so I might as well just grab the XML while I'm at it.
Any thoughts on that? Is it that big of a deal just to grab the data each
and every page load or is there a more elaborate way to handle the caching
in this case?
-Darrel
it. I'd only grab a new copy for the cache when the XML file was physically
updated:
-----------------------------------
Dim XMLfile As String
'check to see if the file exists in the cache
If System.Web.HttpContext.Current.Cache(siteID & "menuXML") Is Nothing Then
'read in the XML file
Dim file As New
System.IO.StreamReader(HttpContext.Current.Server.MapPath("/xml/" & siteID &
"/siteMenu.xml"))
XMLfile = file.ReadToEnd
file.Close()
' set the dependency (to only update when file is changed)
Dim depends As New
System.Web.Caching.CacheDependency(HttpContext.Current.Server.MapPath("/xml/"
& siteID & "/siteMenu.xml"))
'add the text to the cache object
System.Web.HttpContext.Current.Cache.Insert(siteID & "menuXML", XMLfile,
depends)
Else
'just use the cached version
XMLfile = CType(System.Web.HttpContext.Current.Cache(siteID & "menuXML"),
String)
End If
-----------------------------------
For a variety of reasons, we're no longer storing the XML as an XML file.
Instead, we're storing the XML in the database.
For now, that means on each page load, we're querying the DB and grabbing
the XML file. Not a huge deal, but it'd be nice to cache it. However, I
don't see a practical way to cache it and only update when the DB is
updated. It seems to me that to see if the DB is updated, I'd have to query
it anyways, so I might as well just grab the XML while I'm at it.
Any thoughts on that? Is it that big of a deal just to grab the data each
and every page load or is there a more elaborate way to handle the caching
in this case?
-Darrel