473,395 Members | 1,484 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Cacheing DB content

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.Cur rent.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
May 30 '06 #1
6 910
I do something similiar, but I create an object of type XmlDocument.

If you can load your XML document into there and cache you shouldn't
have many problems:

System.Xml.XmlDocument MenuDocument = new XmlDocument();
MenuDocument.Load(Server.MapPath("~" + "\\Menu.xml"));

if you can get your XML from the database to stream into an XmlDocument,
you'll be home free. ;)

darrel wrote:
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.Cur rent.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

May 30 '06 #2
You could cache the data for one minute, or whatever time delay you find
reasonable.

:: At low traffic, the data will be fetched for almost every page, but
that is no problem as there is no strain on the server.

:: At high traffic the data will be fetched at most once a minute.

When you update the data in the database, it will not be more than one
minute before the changes are visible on the site.

Also, the database caches the responses, so most of the time when you
fetch the data you will not even be getting the data from the actual
database, but from the database cache.
darrel wrote:
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.Cur rent.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

May 30 '06 #3
> if you can get your XML from the database to stream into an XmlDocument,
you'll be home free. ;)


So, what does that do...is that making an actual XML document?

If so, how does the cache know when to reload the data?

-Darrel
May 30 '06 #4
> You could cache the data for one minute, or whatever time delay you find
reasonable.
Yea, that's probably what I'll do. It's not ideal, though. The XML file
stores the site menu.

So, a page could be deleted, and if someone visits the site for that one
minute, the old XML file will still be pointing to a non-existant page.

Still, since each page might be accessing the same XML up to 3 times (all in
different controls) it probably makes sense to cache it just to save on
that.
Also, the database caches the responses, so most of the time when you
fetch the data you will not even be getting the data from the actual
database, but from the database cache.


Ah, so if I have 3 different calls to get the XML from the DB in three
different controls (all accessing the same class), does caching benefit me
at all, or is that all being done by the DB for me?

-Darrel
May 30 '06 #5
darrel wrote:
You could cache the data for one minute, or whatever time delay you find
reasonable.
Yea, that's probably what I'll do. It's not ideal, though. The XML file
stores the site menu.

So, a page could be deleted, and if someone visits the site for that one
minute, the old XML file will still be pointing to a non-existant page.


Yes, but will have that problem anyway. The user could visit the site
just before the page was deleted, and until the page in the browser gets
refreshed, it will have a menu with an item that points to a
non-existant page.
Still, since each page might be accessing the same XML up to 3 times (all in
different controls) it probably makes sense to cache it just to save on
that.


It definitely does. Even an extremely short cache time, like a few
seconds, would still save some database traffic.
Also, the database caches the responses, so most of the time when you
fetch the data you will not even be getting the data from the actual
database, but from the database cache.


Ah, so if I have 3 different calls to get the XML from the DB in three
different controls (all accessing the same class), does caching benefit me
at all, or is that all being done by the DB for me?


Yes, you would benefit from caching. Even if the database caches the
result, you still have to make the roundtrip to the database server to
get it.

You only need to decide if you benefit enough, or if the performance is
good enough without bothering with the caching.
May 30 '06 #6
> Yes, but will have that problem anyway. The user could visit the site just
before the page was deleted, and until the page in the browser gets
refreshed, it will have a menu with an item that points to a non-existant
page.


Touche! (Good point! ;o)

-Darrel
May 30 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Clive Backham | last post by:
I tried posting this on comp.infosystems.www.misc, but that group appears to get very little traffic. So now I'm trying here. If there is a more appropriate group, please let me know. I'm...
3
by: tisgrim | last post by:
Right...have loads of C++, SQLServer experience but about 5 minutes of JavaScript, ASP etc. Spent a frustrating 2 days deciding the best way to get some data from a SQL database, then cached in...
12
by: Sunny | last post by:
Hi, I need to download some web pages from a web server (most of them are not static html, but .asp pages). The content (I.e. the pure html) in the pages are not changed very often, so I'll...
8
by: roy anderson | last post by:
Hey all, Thought I had this figured out a week ago, but evidently not. Basically when I publish a site in 2.0, the images aren't cacheing? For instance, let's say I created a page with a...
3
by: visu | last post by:
I am currently working on my personl website a completely DB driven web application. in that i ve updating the images thru my admin panel .. but i am getting the old images not the update one when...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.