HighTechTalks DotNet Forums  

Cache.Insert set absolute time but Cache become null immediately

ASP.net Caching microsoft.public.dotnet.framework.aspnet.caching


Discuss Cache.Insert set absolute time but Cache become null immediately in the ASP.net Caching forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Robin
 
Posts: n/a

Default Cache.Insert set absolute time but Cache become null immediately - 02-06-2007 , 11:20 AM






What am I doing wrong here? I am trying to Cache data for 5 minutes (absolute
time). To test caching, I am writing to a log file every time the application
determines that the Cache item is null and needs to pull the data from the
database again. I run my asp.net form, then I hit refresh over and over. It
logs that it is hitting the database for data because the cache is null. The
cache should contain my data for 5 minutes - therefore, it should not call to
the database. Here is my code (without the logging):

string cacheKey = "CachedDomainDataSet";
object cacheObject = Cache[cacheKey] as DataSet;
if(cacheObject == null)
{
cacheObject = DMSProcessing.FillDomainDataSet();//CALL DATABASE
if(cacheObject != null)
{
//I've tried everything I can think of here. My last attempt has been to
CacheItemPriority to NotRemovable.

Cache.Insert(cacheKey, cacheObject, null, DateTime.Now.AddMinutes(5),
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.NotRemovable, null);
}





Reply With Quote
  #2  
Old   
Robin
 
Posts: n/a

Default RE: Cache.Insert set absolute time but Cache become null immediately - 02-06-2007 , 01:10 PM






This code works when I test it locally (http://localhost/myapp). It works
perfect. When I run it on our test server, the Cache object just isn't there,
it's always null. There must be a server setting I am missing. The
application runs in its own application pool on the test server.

"Robin" wrote:

Quote:
What am I doing wrong here? I am trying to Cache data for 5 minutes (absolute
time). To test caching, I am writing to a log file every time the application
determines that the Cache item is null and needs to pull the data from the
database again. I run my asp.net form, then I hit refresh over and over. It
logs that it is hitting the database for data because the cache is null. The
cache should contain my data for 5 minutes - therefore, it should not call to
the database. Here is my code (without the logging):

string cacheKey = "CachedDomainDataSet";
object cacheObject = Cache[cacheKey] as DataSet;
if(cacheObject == null)
{
cacheObject = DMSProcessing.FillDomainDataSet();//CALL DATABASE
if(cacheObject != null)
{
//I've tried everything I can think of here. My last attempt has been to
CacheItemPriority to NotRemovable.

Cache.Insert(cacheKey, cacheObject, null, DateTime.Now.AddMinutes(5),
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.NotRemovable, null);
}





Reply With Quote
  #3  
Old   
Walter Wang [MSFT]
 
Posts: n/a

Default RE: Cache.Insert set absolute time but Cache become null immediately - 02-07-2007 , 12:37 AM



Hi Robin,

Is it possible that DMSProcessing.FillDomainDataSet() is not returning a
DataSet? From your code, you declared cacheObject as object, and you used
"as DataSet" to read the cache. If it's not a DataSet, the cacheObject will
always be null.


Sincerely,
Walter Wang (wawang (AT) online (DOT) microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.


Reply With Quote
  #4  
Old   
Robin
 
Posts: n/a

Default RE: Cache.Insert set absolute time but Cache become null immediate - 02-08-2007 , 11:12 AM



Yes, I see your point, but I do have data coming back from the database
always (in my test environment). I see the data fill up my combobox, so I
know it's there.

"Walter Wang [MSFT]" wrote:

Quote:
Hi Robin,

Is it possible that DMSProcessing.FillDomainDataSet() is not returning a
DataSet? From your code, you declared cacheObject as object, and you used
"as DataSet" to read the cache. If it's not a DataSet, the cacheObject will
always be null.


Sincerely,
Walter Wang (wawang (AT) online (DOT) microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.



Reply With Quote
  #5  
Old   
Walter Wang [MSFT]
 
Posts: n/a

Default RE: Cache.Insert set absolute time but Cache become null immediate - 02-09-2007 , 01:39 AM



Hi Robin,

Have you tried to read the cached object immdiately after you put it into
the cache? Is it there?

Another thing to check is receive the cache removing callback notification
and check the CacheItemRemovedReason:

protected void Page_Load(object sender, EventArgs e)
{
string cacheKey = "CachedDomainDataSet";
object cacheObject = Cache[cacheKey] as DataSet;
if (cacheObject == null)
{
Response.Write("Creating new cache");
cacheObject = new DataSet();
if (cacheObject != null)
{
Cache.Insert(cacheKey, cacheObject, null,
DateTime.Now.AddMinutes(5),
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.NotRemovable,
cache_Removed);
}
}
}

private void cache_Removed(string key, Object value,
CacheItemRemovedReason reason)
{

}

For the caching related config, you can find it here:

#cache Element for caching (ASP.NET Settings Schema)
http://msdn2.microsoft.com/en-us/library/ms228248.aspx

You may check if your existing configuration has related information to
this.

Here's also some articles you may find useful:

#John's Adventures: Why Does My ASP.NET Cache Keep Clearing Itself?
http://www.johnsadventures.com/archi...pnet_cache_kee
p_clearing_i.html


Regards,
Walter Wang (wawang (AT) online (DOT) microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.


Reply With Quote
  #6  
Old   
Robin
 
Posts: n/a

Default RE: Cache.Insert set absolute time but Cache become null immediate - 02-09-2007 , 03:39 PM



is <cache> and/or <caching> new in .NET 2.0? I'm on 1.1 and the config file
can't understand these entries.

I've not gotten a removed reason other than 'Removed'.

It seems there are many cache objects being created because when I log when
the cache is being removed (occurs when I force recycle the application
pool), up to 5 cache items are removed. Am I suppossed to create a singleton
Cache object?



"Walter Wang [MSFT]" wrote:

Quote:
Hi Robin,

Have you tried to read the cached object immdiately after you put it into
the cache? Is it there?

Another thing to check is receive the cache removing callback notification
and check the CacheItemRemovedReason:

protected void Page_Load(object sender, EventArgs e)
{
string cacheKey = "CachedDomainDataSet";
object cacheObject = Cache[cacheKey] as DataSet;
if (cacheObject == null)
{
Response.Write("Creating new cache");
cacheObject = new DataSet();
if (cacheObject != null)
{
Cache.Insert(cacheKey, cacheObject, null,
DateTime.Now.AddMinutes(5),
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.NotRemovable,
cache_Removed);
}
}
}

private void cache_Removed(string key, Object value,
CacheItemRemovedReason reason)
{

}

For the caching related config, you can find it here:

#cache Element for caching (ASP.NET Settings Schema)
http://msdn2.microsoft.com/en-us/library/ms228248.aspx

You may check if your existing configuration has related information to
this.

Here's also some articles you may find useful:

#John's Adventures: Why Does My ASP.NET Cache Keep Clearing Itself?
http://www.johnsadventures.com/archi...pnet_cache_kee
p_clearing_i.html


Regards,
Walter Wang (wawang (AT) online (DOT) microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.



Reply With Quote
  #7  
Old   
Walter Wang [MSFT]
 
Posts: n/a

Default RE: Cache.Insert set absolute time but Cache become null immediate - 02-12-2007 , 04:35 AM



Hi Robin,

This <cache> element are new for .NET 2.0.

Regarding the removed reason 'Removed':

========
(from CacheItemRemovedReason enumeration documentation)

Removed The item is removed from the cache by a Remove method call or by an
Insert method call that specified the same key.
========

This is expected since you're repeatedly inserting the same name cache
object, which causes the previous one get removed. The root cause here is
still that following code returns null on your server:

object cacheObject = Cache[cacheKey] as DataSet;

Since the CacheItemRemovedReason is 'Removed', which means it's actually
there, with all due respect, I really cannot think of another reason other
than "Cache[cacheKey] as DataSet" is null. Would you please help me double
check that by declaring "cacheObject" as DataSet instead of Object?

If this is not the case, I'm afraid you will have to contact Microsoft
Customer Support and Service for live debugging or dump analysis to
troubleshoot such issue.



Regards,
Walter Wang (wawang (AT) online (DOT) microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.


Reply With Quote
  #8  
Old   
Robin
 
Posts: n/a

Default RE: Cache.Insert set absolute time but Cache become null immediate - 02-13-2007 , 05:40 PM



I updated my code to only deal with objects, no dataset. I did a thorough
test of the caching on my local machine (IIS 5.1) and on our web server (IIS
6). The local machine only caches after my absolute time and reason for
removal is 'Expired'. The web server, on the other hand, adds the object to
cache much more often than my absolute time.

I am wonder if it is the location of my code. It is in the Page_Load event.
Should it be in global.asax?

The server caching seems to create multiple cache items. How can I use one
cache object? Here are the logs from the server (each posting is a page
refresh or new page request):

2/13/2007 2:22:11 PM Cache was null, added object to Cache. Current time
setting in minutes is: 10
2/13/2007 2:22:27 PM Cache was null, added object to Cache. Current time
setting in minutes is: 10
2/13/2007 2:22:41 PM Cache was null, added object to Cache. Current time
setting in minutes is: 10
2/13/2007 2:22:55 PM The cache got called. It's value is: System.Object
2/13/2007 2:23:17 PM The cache got called. It's value is: System.Object
2/13/2007 2:23:37 PM The cache got called. It's value is: System.Object
2/13/2007 2:24:08 PM The cache got called. It's value is: System.Objec
2/13/2007 2:24:40 PM The cache got called. It's value is: System.Object
2/13/2007 2:25:12 PM The cache got called. It's value is: System.Object
2/13/2007 2:25:15 PM The cache got called. It's value is: System.Object
2/13/2007 2:27:05 PM Cache was null, added object to Cache. Current time
setting in minutes is: 10
2/13/2007 2:28:01 PM The cache got called. It's value is: System.Object
2/13/2007 2:32:41 PM The cache got called. It's value is: System.Object
2/13/2007 2:32:46 PM An object was removed from cache:
KEY: TestObjectInCache
obj removed: System.Object
Removed reason: Expired

2/13/2007 2:32:46 PM Cache was null, added object to Cache. Current time
setting in minutes is: 10
2/13/2007 2:33:00 PM An object was removed from cache:
KEY: TestObjectInCache
obj removed: System.Object
Removed reason: Expired

2/13/2007 2:33:00 PM An object was removed from cache:
KEY: TestObjectInCache
obj removed: System.Object
Removed reason: Expired

2/13/2007 2:33:53 PM Cache was null, added object to Cache. Current time
setting in minutes is: 10
2/13/2007 2:34:00 PM The cache got called. It's value is: System.Object


"Walter Wang [MSFT]" wrote:

Quote:
Hi Robin,

This <cache> element are new for .NET 2.0.

Regarding the removed reason 'Removed':

========
(from CacheItemRemovedReason enumeration documentation)

Removed The item is removed from the cache by a Remove method call or by an
Insert method call that specified the same key.
========

This is expected since you're repeatedly inserting the same name cache
object, which causes the previous one get removed. The root cause here is
still that following code returns null on your server:

object cacheObject = Cache[cacheKey] as DataSet;

Since the CacheItemRemovedReason is 'Removed', which means it's actually
there, with all due respect, I really cannot think of another reason other
than "Cache[cacheKey] as DataSet" is null. Would you please help me double
check that by declaring "cacheObject" as DataSet instead of Object?

If this is not the case, I'm afraid you will have to contact Microsoft
Customer Support and Service for live debugging or dump analysis to
troubleshoot such issue.



Regards,
Walter Wang (wawang (AT) online (DOT) microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.



Reply With Quote
  #9  
Old   
Walter Wang [MSFT]
 
Posts: n/a

Default RE: Cache.Insert set absolute time but Cache become null immediate - 02-14-2007 , 06:54 AM



Hi Robin,

Please help me test following simple code on your server:

private void Page_Load(object sender, System.EventArgs e)
{
string cached = Cache["test"] as string;
if (cached == null)
{
Response.Write("Not cached, inserting...");
Cache.Insert("test", DateTime.Now.ToString(), null,
DateTime.Now.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.NotRemovable, null);
}
else
{
Response.Write("Cached: " + cached);
}
}

Try to visit the page and refresh for several times, and tell me the
result. Thanks.

Regards,
Walter Wang (wawang (AT) online (DOT) microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.


Reply With Quote
  #10  
Old   
Robin
 
Posts: n/a

Default RE: Cache.Insert set absolute time but Cache become null immediate - 02-14-2007 , 12:33 PM



Here is the code:
//test code per msdn forum
string cached = Cache["test"] as string;
if(cached == null)
{
LogTestCacheMessage("Not cached, inserting into cache");
Cache.Insert("test", DateTime.Now.ToString(), null,
DateTime.Now.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.NotRemovable, null);
}
else
{
LogTestCacheMessage("Cached: " + cached);
}


Here are the logs.

2/14/2007 9:03:08 AM Not cached, inserting into cache
2/14/2007 9:03:29 AM Not cached, inserting into cache
2/14/2007 9:03:44 AM Cached: 2/14/2007 9:03:29 AM
2/14/2007 9:03:57 AM Cached: 2/14/2007 9:03:29 AM
2/14/2007 9:04:03 AM Cached: 2/14/2007 9:03:29 AM
2/14/2007 9:04:24 AM Cached: 2/14/2007 9:03:29 AM
2/14/2007 9:04:31 AM Cached: 2/14/2007 9:03:08 AM
2/14/2007 9:04:52 AM Cached: 2/14/2007 9:03:29 AM
2/14/2007 9:09:51 AM Not cached, inserting into cache
2/14/2007 9:09:55 AM Not cached, inserting into cache
2/14/2007 9:10:39 AM Cached: 2/14/2007 9:09:55 AM
2/14/2007 9:10:43 AM Not cached, inserting into cache
2/14/2007 9:11:59 AM Not cached, inserting into cache
2/14/2007 9:12:04 AM Cached: 2/14/2007 9:10:43 AM
2/14/2007 9:12:06 AM Cached: 2/14/2007 9:10:43 AM
2/14/2007 9:12:19 AM Cached: 2/14/2007 9:10:43 AM
2/14/2007 9:13:45 AM Not cached, inserting into cache
2/14/2007 9:13:53 AM Cached: 2/14/2007 9:09:55 AM
2/14/2007 9:14:39 AM Cached: 2/14/2007 9:13:45 AM
2/14/2007 9:18:50 AM Not cached, inserting into cache
2/14/2007 9:18:52 AM Not cached, inserting into cache
2/14/2007 9:19:05 AM Not cached, inserting into cache
2/14/2007 9:19:52 AM Cached: 2/14/2007 9:18:52 AM
2/14/2007 9:19:59 AM Cached: 2/14/2007 9:19:05 AM
2/14/2007 9:26:42 AM Not cached, inserting into cache
2/14/2007 9:26:48 AM Not cached, inserting into cache


Thanks.

"Walter Wang [MSFT]" wrote:

Quote:
Hi Robin,

Please help me test following simple code on your server:

private void Page_Load(object sender, System.EventArgs e)
{
string cached = Cache["test"] as string;
if (cached == null)
{
Response.Write("Not cached, inserting...");
Cache.Insert("test", DateTime.Now.ToString(), null,
DateTime.Now.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.NotRemovable, null);
}
else
{
Response.Write("Cached: " + cached);
}
}

Try to visit the page and refresh for several times, and tell me the
result. Thanks.

Regards,
Walter Wang (wawang (AT) online (DOT) microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.