473,324 Members | 2,511 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,324 software developers and data experts.

Followup on Application Cache question...

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
5 1716
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
> 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
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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Keith Chadwick | last post by:
I am having some trouble referencing an Application("myVar") variable from within a module.vb file on my ASP.NET site. According to the documentation I should be able to reference...
5
by: stefano mostarda | last post by:
Hi there, I have a question. In my application I have a table which is frequently used to fill a combo. I want to store it in the application or cache object. it is never updated. What's...
3
by: spammy | last post by:
hi all, im currently writing an ASP.net front end to display data found in an XML file. the XML file is quite large (700k or so), and so takes a while to load. it will be changing around every...
7
by: Greg Collins [MVP] | last post by:
Hi, I couldn't find what I was looking for by searching the newsgroup, but perhaps these have already been discussed somewhere. This is a bit long with a lot of interrelated questions. What I've...
4
by: Trevor Andrew | last post by:
Hi There, Hopefully this isn't too difficult a question to express here. I have a 3 tier application. 1. Presentation Tier: ASP.NET web application. 2. Middle Tier: ASP.NET Web Services that...
4
by: Fred Nelson | last post by:
Hi: This is probably a newby question however I can't figure it out! I'm trying to use application data caching in a web application. I have read an article on 4 guys from rolla that shows...
2
by: LeAnne | last post by:
My question is to do with scalability and the location for storate of cache, session, viewstate and application data. Application - Data is stored in memory (in process) Session - Data is stored...
5
by: J055 | last post by:
Hi The following code works on my develeopment machine using the VS web server. When I run the application on 2 other Windows 2003/IIS 6 servers no caching seems to take place at all. Can...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.