By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,829 Members | 1,829 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,829 IT Pros & Developers. It's quick & easy.

Followup on Application Cache question...

P: n/a
I thought this warranted a new thread.

Yesterday I asked about access relatively static content...is it better to
read from the DB, or just grab a text file. It was suggested that I use the
DB and look into the Application Cache settings.

I found a good article here:

http://www.developer.com/net/net/article.php/1477771

It mentions that, at least with files, you can set the cache to retain the
information until the file is updated, at which time it will access it again
and re-cache. Can the same be done for a DB? My guess is no, since to check
for the update, it'd have to access the DB, and if it has to do that, it
might as well grab the data while its there.

The catch is that the text in question, though it won't change often, IS
editable via an admin tool and when it's changed, the client would obviously
want to see the change take place...instead of waiting for the cache to
expire. I could set the cache to something like 5 minutes, which might be
acceptable.

Anyways, the final thought was to keep the data in the DB for editing
purposes, but upon each 'save', also write out a text file and then read
that text file in via asp, setting the cache to renew only when the file is
again changed. Does anyone see a problem with that logic?

-Darrel
Nov 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
ASP.Net 2.0 when used with SQL Server 2005 will support cache invalidation.
I believe there are ways to do it in ASP.Net 1.1 but they aren't
straightforward
(http://msdn.microsoft.com/library/de...-us/dnaspnet/h
tml/asp11022004.asp)

Caching something for 5 minutes, which is read a lot in that time will
actually give you a VERY good performance boost....if you have 10 page views
per second, and 10% of those use that data, you'll save like 299 database
calls (pretty good!)

An alternative to writing a file and putting a dependency on it, is to
simply have a nice business layer which clears the cache on update, here's
some pseudo-code:

Function GetData
If NOT in cache THEN
Get From DataBase
Store In Cache
End IF

Return Data
End Function
Function UpdataData
Update The Data
Clear Cache
End function

you can even do something like

public DataSet GetData(){
return GetData(false);
}
private DataSet GetData(bool forceRefresh){
string cacheKey = "GetAllData";
DataSet ds = (DataSet) HttpRuntime.Cache[cacheKey];
if (ds == null || forceRefresh){
//get data
//store in cache
}
return ds;
}

public void UpdateData(){
//updateData
GetData(true);
}
this code will automatically refetch the new data and store it in the cache
when you update...sweet :)

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Darrel" <no*****@nospam.com> wrote in message
news:uW*************@TK2MSFTNGP14.phx.gbl...
I thought this warranted a new thread.

Yesterday I asked about access relatively static content...is it better to
read from the DB, or just grab a text file. It was suggested that I use the DB and look into the Application Cache settings.

I found a good article here:

http://www.developer.com/net/net/article.php/1477771

It mentions that, at least with files, you can set the cache to retain the
information until the file is updated, at which time it will access it aga in and re-cache. Can the same be done for a DB? My guess is no, since to check for the update, it'd have to access the DB, and if it has to do that, it
might as well grab the data while its there.

The catch is that the text in question, though it won't change often, IS
editable via an admin tool and when it's changed, the client would obviously want to see the change take place...instead of waiting for the cache to
expire. I could set the cache to something like 5 minutes, which might be
acceptable.

Anyways, the final thought was to keep the data in the DB for editing
purposes, but upon each 'save', also write out a text file and then read
that text file in via asp, setting the cache to renew only when the file is again changed. Does anyone see a problem with that logic?

-Darrel

Nov 18 '05 #2

P: n/a
> Caching something for 5 minutes, which is read a lot in that time will
actually give you a VERY good performance boost....if you have 10 page
views
per second, and 10% of those use that data, you'll save like 299 database
calls (pretty good!)
Yea...but there's no way this site is going to see enough traffic to really
see the benefit of that. ;o)

Still, that looks like perhaps the most viable option right now. These
snippets of text will be updated so infrequently that the client can easily
wait 5 minutes every now and again.
Function UpdataData
Update The Data
Clear Cache
End function
Ah...OK, that makes sense. Instead of writing to a file, simply tell the
cache to come and refresh itself.
you can even do something like
public void UpdateData(){
//updateData
GetData(true);
}


I'm a little confused on that one. Is the DS a global one? I guess the
question is how/where do you set the 'update key' from the usercontrol that
updates the data so that the usercontrol that gets the data can check for
it? Is that some sort of globally read variable?

-Darrel
Nov 18 '05 #3

P: n/a
DataSet isn't a global variable...this is a business layer...whenever you
want the data, you call the function GetData() which takes care of checking
the cache, getting the data from the database if not in the cache and
storing it in the cache, then returning it.

function DataSet GetData(bool forceRefresh){
string cacheKey = "GetData";
DataSet ds = (DataSet)HttpRuntime.Cache[cacheKey];
if (ds == null || forceRefresh){
ds = GetDataFromDataBase();
HttpRuntime.Cache.Insert(cacheKey, ds, null,
DateTime.Now.AddHours(12), TimeSpan.Zero);
}
return ds;
}
The user control simply calls GetData() whenever it needs the data:

MyRepeater.DataSource = GetData();
MyRepeater.DataBind();

or

DataSet ds = GetData();
for (int i = 0; i < ds.Tables[0].Rows.Count; ++i){
///
}

or whatever you do with it.
GetData() will always have fresh data in it, because when your other user
control calls UpdateData(), it'll cause the cache to get wiped and GetData()
will go hit the database..

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Darrel" <no*****@nospam.com> wrote in message
news:eR**************@TK2MSFTNGP10.phx.gbl...
Caching something for 5 minutes, which is read a lot in that time will
actually give you a VERY good performance boost....if you have 10 page
views
per second, and 10% of those use that data, you'll save like 299 database calls (pretty good!)
Yea...but there's no way this site is going to see enough traffic to

really see the benefit of that. ;o)

Still, that looks like perhaps the most viable option right now. These
snippets of text will be updated so infrequently that the client can easily wait 5 minutes every now and again.
Function UpdataData
Update The Data
Clear Cache
End function
Ah...OK, that makes sense. Instead of writing to a file, simply tell the
cache to come and refresh itself.
you can even do something like
public void UpdateData(){
//updateData
GetData(true);
}


I'm a little confused on that one. Is the DS a global one? I guess the
question is how/where do you set the 'update key' from the usercontrol

that updates the data so that the usercontrol that gets the data can check for
it? Is that some sort of globally read variable?

-Darrel

Nov 18 '05 #4

P: n/a
> GetData() will always have fresh data in it, because when your other user
control calls UpdateData(), it'll cause the cache to get wiped and
GetData()
will go hit the database..


That was the question I had.

You have this:

Function UpdataData
Update The Data
Clear Cache
End function

But what, exactly is 'cache'? In otherwords, how to you reference a specific
cache to tell it to clear? Is the cache a global object of some sort that
can be referenced between controls?

-Darrel
Nov 18 '05 #5

P: n/a
Well, you saw how I got something from the cache:

string cacheKey = "GetData";
DataSet ds = (DataSet)HttpRuntime.Cache[cacheKey];
and how I put something in the cache:

HttpRuntime.Cache.Insert(cacheKey, ds, null, DateTime.Now.AddHours(12),
TimeSpan.Zero);
You clear something from the cache via:

HttpRuntime.Cache.Remove("KEY");
the "KEY" is the same as the cacheKey above...you could make it a private
const member of your class:

public class DataUtility{
private const string cacheKey = "GetData";

..
...
}

and use this cacheKey across your classes..
As for the cache itself, HttpRuntime.Cache is created and maintained by
ASP.Net. When you are in a page, you can simply use Cache or Page.Cache to
access it. You only need to use HttpRuntime.Cache when you aren't in a
page/user control (when ur in just a normal class)...but they refer to the
same thing.

Some resources:
http://openmymind.net/DataStorage/index.html#cache
http://msdn.microsoft.com/library/de...classtopic.asp
http://www.developer.com/net/net/article.php/1477771

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl...
DataSet isn't a global variable...this is a business layer...whenever you
want the data, you call the function GetData() which takes care of checking the cache, getting the data from the database if not in the cache and
storing it in the cache, then returning it.

function DataSet GetData(bool forceRefresh){
string cacheKey = "GetData";
DataSet ds = (DataSet)HttpRuntime.Cache[cacheKey];
if (ds == null || forceRefresh){
ds = GetDataFromDataBase();
HttpRuntime.Cache.Insert(cacheKey, ds, null,
DateTime.Now.AddHours(12), TimeSpan.Zero);
}
return ds;
}
The user control simply calls GetData() whenever it needs the data:

MyRepeater.DataSource = GetData();
MyRepeater.DataBind();

or

DataSet ds = GetData();
for (int i = 0; i < ds.Tables[0].Rows.Count; ++i){
///
}

or whatever you do with it.
GetData() will always have fresh data in it, because when your other user
control calls UpdateData(), it'll cause the cache to get wiped and GetData() will go hit the database..

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Darrel" <no*****@nospam.com> wrote in message
news:eR**************@TK2MSFTNGP10.phx.gbl...
Caching something for 5 minutes, which is read a lot in that time will
actually give you a VERY good performance boost....if you have 10 page
views
per second, and 10% of those use that data, you'll save like 299 database calls (pretty good!)


Yea...but there's no way this site is going to see enough traffic to

really
see the benefit of that. ;o)

Still, that looks like perhaps the most viable option right now. These
snippets of text will be updated so infrequently that the client can

easily
wait 5 minutes every now and again.
Function UpdataData
Update The Data
Clear Cache
End function


Ah...OK, that makes sense. Instead of writing to a file, simply tell the
cache to come and refresh itself.
you can even do something like
public void UpdateData(){
//updateData
GetData(true);
}


I'm a little confused on that one. Is the DS a global one? I guess the
question is how/where do you set the 'update key' from the usercontrol

that
updates the data so that the usercontrol that gets the data can check for it? Is that some sort of globally read variable?

-Darrel


Nov 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.