ASPNET cache cannot refresh

G

Guest

I used
HttpContext.Current.Cache["Categories"] To cache data from database.
The code is like that.

public static DataView GetCategories() {
if ( HttpContext.Current.Cache["Categories"] == null ) {
HttpContext.Current.Cache["Categories"] = GetCategoriesFromDB();
}
return (DataView)HttpContext.Current.Cache["Categories"];}

However when database data changed, the page still keept the cached data no
matter how many times I refresh the page. I closed the browser, and reopened
it, and it had no effect. Finally I restarted the machine, the data then
refreshed. Is that the correct way for ASPNET cache work?
 
L

Lars-Erik Aabech

Hi!

If you use the HttpContext.Current.Cache.Add(...) method instead, you can
specify the length of time before the cached object is reset. See the
documentation here:
http://msdn.microsoft.com/library/e...emwebcachingcacheclassaddtopic.asp?frame=true

If you only set the item by using the indexer, you cache the item forever or
until the application is reset.
You could, however, write some code in the page that updates the data, that
resets the cache object again. That would keep the cache syncronized at all
times.

HTH,
Lars-Erik
 
J

Joe Fallon

The cache knows nothing about your changes to data in the DB.

You have to make some choices.
1. Expire the cache periodically and live with possibly stale data until it
is refreshed.

2. Use the new 2.0 stuff for invalidating the cache based on changes to the
DB.
(I have not seen it yet.)

3. Use code to poll the DB for changes and then invalidate the cache.
I use this technique. Works well.

See this article for how to do it: (note my comments on the bootm of the
page.)
http://www.eggheadcafe.com/articles/20040607.asp
 
S

Steven Cheng[MSFT]

Thanks for your input Erik,

Hi Charts,

As Erik has mentioned, we are recommended to use the ASP.NET's Application
Cache through the

HttpContext.Cache.Add(.....) (or Insert method) rather than simply use the

Cache["key"] = value.....

since using the Cache["key"] will cause the added object be persisted in
the memory and won't be removed until the application restart.......

And by using the Cache.Add method, we can add expire policy for the cached
object, such as TimeSpan or FileDependency.

Here are some msdn reference which may also be helpful:

http://msdn.microsoft.com/msdnmag/issues/04/07/cuttingedge/default.aspx

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconcacheapis.asp

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


--------------------
| Reply-To: "Lars-Erik Aabech" <[email protected]>
| From: "Lars-Erik Aabech" <[email protected]>
| References: <[email protected]>
| Subject: Re: ASPNET cache cannot refresh
| Date: Wed, 6 Jul 2005 17:16:16 +0200
| Lines: 37
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| Message-ID: <#[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: host-81-191-131-56.bluecom.no 81.191.131.56
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:110651
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Hi!
|
| If you use the HttpContext.Current.Cache.Add(...) method instead, you can
| specify the length of time before the cached object is reset. See the
| documentation here:
|
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwebcachingcach
eclassaddtopic.asp?frame=true
|
| If you only set the item by using the indexer, you cache the item forever
or
| until the application is reset.
| You could, however, write some code in the page that updates the data,
that
| resets the cache object again. That would keep the cache syncronized at
all
| times.
|
| HTH,
| Lars-Erik
|
| | >I used
| > HttpContext.Current.Cache["Categories"] To cache data from database.
| > The code is like that.
| >
| > public static DataView GetCategories() {
| > if ( HttpContext.Current.Cache["Categories"] == null ) {
| > HttpContext.Current.Cache["Categories"] = GetCategoriesFromDB();
| > }
| > return (DataView)HttpContext.Current.Cache["Categories"];}
| >
| > However when database data changed, the page still keept the cached
data
| > no
| > matter how many times I refresh the page. I closed the browser, and
| > reopened
| > it, and it had no effect. Finally I restarted the machine, the data
then
| > refreshed. Is that the correct way for ASPNET cache work?
| >
|
|
|
 
G

Guest

Thanks

Joe Fallon said:
The cache knows nothing about your changes to data in the DB.

You have to make some choices.
1. Expire the cache periodically and live with possibly stale data until it
is refreshed.

2. Use the new 2.0 stuff for invalidating the cache based on changes to the
DB.
(I have not seen it yet.)

3. Use code to poll the DB for changes and then invalidate the cache.
I use this technique. Works well.

See this article for how to do it: (note my comments on the bootm of the
page.)
http://www.eggheadcafe.com/articles/20040607.asp

--
Joe Fallon



Charts said:
I used
HttpContext.Current.Cache["Categories"] To cache data from database.
The code is like that.

public static DataView GetCategories() {
if ( HttpContext.Current.Cache["Categories"] == null ) {
HttpContext.Current.Cache["Categories"] = GetCategoriesFromDB();
}
return (DataView)HttpContext.Current.Cache["Categories"];}

However when database data changed, the page still keept the cached data
no
matter how many times I refresh the page. I closed the browser, and
reopened
it, and it had no effect. Finally I restarted the machine, the data then
refreshed. Is that the correct way for ASPNET cache work?
 
G

Guest

Thanks guys, It is very helpful. Charts

Lars-Erik Aabech said:
Hi!

If you use the HttpContext.Current.Cache.Add(...) method instead, you can
specify the length of time before the cached object is reset. See the
documentation here:
http://msdn.microsoft.com/library/e...emwebcachingcacheclassaddtopic.asp?frame=true

If you only set the item by using the indexer, you cache the item forever or
until the application is reset.
You could, however, write some code in the page that updates the data, that
resets the cache object again. That would keep the cache syncronized at all
times.

HTH,
Lars-Erik

Charts said:
I used
HttpContext.Current.Cache["Categories"] To cache data from database.
The code is like that.

public static DataView GetCategories() {
if ( HttpContext.Current.Cache["Categories"] == null ) {
HttpContext.Current.Cache["Categories"] = GetCategoriesFromDB();
}
return (DataView)HttpContext.Current.Cache["Categories"];}

However when database data changed, the page still keept the cached data
no
matter how many times I refresh the page. I closed the browser, and
reopened
it, and it had no effect. Finally I restarted the machine, the data then
refreshed. Is that the correct way for ASPNET cache work?
 
S

Steven Cheng[MSFT]

You're welcome :)

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

--------------------
| Thread-Topic: ASPNET cache cannot refresh
| thread-index: AcWC9DvqzHvzcR6hQQW2FfwztoqTtg==
| X-WBNR-Posting-Host: 216.64.114.162
| From: =?Utf-8?B?Q2hhcnRz?= <[email protected]>
| References: <[email protected]>
<#[email protected]>
| Subject: Re: ASPNET cache cannot refresh
| Date: Thu, 7 Jul 2005 06:03:13 -0700
| Lines: 42
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:110902
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Thanks guys, It is very helpful. Charts
|
| "Lars-Erik Aabech" wrote:
|
| > Hi!
| >
| > If you use the HttpContext.Current.Cache.Add(...) method instead, you
can
| > specify the length of time before the cached object is reset. See the
| > documentation here:
| >
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwebcachingcach
eclassaddtopic.asp?frame=true
| >
| > If you only set the item by using the indexer, you cache the item
forever or
| > until the application is reset.
| > You could, however, write some code in the page that updates the data,
that
| > resets the cache object again. That would keep the cache syncronized at
all
| > times.
| >
| > HTH,
| > Lars-Erik
| >
| > | > >I used
| > > HttpContext.Current.Cache["Categories"] To cache data from database.
| > > The code is like that.
| > >
| > > public static DataView GetCategories() {
| > > if ( HttpContext.Current.Cache["Categories"] == null ) {
| > > HttpContext.Current.Cache["Categories"] = GetCategoriesFromDB();
| > > }
| > > return (DataView)HttpContext.Current.Cache["Categories"];}
| > >
| > > However when database data changed, the page still keept the cached
data
| > > no
| > > matter how many times I refresh the page. I closed the browser, and
| > > reopened
| > > it, and it had no effect. Finally I restarted the machine, the data
then
| > > refreshed. Is that the correct way for ASPNET cache work?
| > >
| >
| >
| >
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

aspnet unleashed store 0
Page data caching 1
cache 2
cache 2
Refresh button 5
Thread safe singleton to access the cache? 1
Cache not working as expected when using list(of integer) 2
Dataset, refresh, Cache? 1

Members online

Forum statistics

Threads
473,787
Messages
2,569,631
Members
45,338
Latest member
41Pearline46

Latest Threads

Top