[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.aspnet.caching

Cache.Insert set absolute time but Cache become null immediately

Robin

2/6/2007 4:20:00 PM

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);
}




11 Answers

Robin

2/6/2007 6:10:00 PM

0

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:

> 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);
> }
>
>
>
>

wawang

2/7/2007 5:37:00 AM

0

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@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default....
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/subscriptions/support/de....
==================================================

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

Robin

2/8/2007 4:12:00 PM

0

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:

> 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@online.microsoft.com, remove 'online.')
> Microsoft Online Community Support
>
> ==================================================
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/subscriptions/managednewsgroups/default....
> 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/subscriptions/support/de....
> ==================================================
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>

wawang

2/9/2007 6:40:00 AM

0

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/ms2...

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/archives/2006/02/why_does_my_aspnet...
p_clearing_i.html


Regards,
Walter Wang (wawang@online.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.

Robin

2/9/2007 8:39:00 PM

0

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:

> 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/ms2...
>
> 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/archives/2006/02/why_does_my_aspnet...
> p_clearing_i.html
>
>
> Regards,
> Walter Wang (wawang@online.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.
>
>

wawang

2/12/2007 9:36:00 AM

0

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@online.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.

Robin

2/13/2007 10:40:00 PM

0

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:

> 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@online.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.
>
>

wawang

2/14/2007 11:54:00 AM

0

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@online.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.

Robin

2/14/2007 5:34:00 PM

0

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:

> 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@online.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.
>
>

wawang

2/15/2007 9:07:00 AM

0

Hi Robin,

Thank you for your detailed log information. This helps me to see more
clearly now.

It seems you're using Web Garden mode in IIS -- multiple worker process for
an AppPool.

Because Web gardens enable the use of multiple processes, all processes
have their
own copy of application state, in-process session state, caches, and static
data.

By default, the Web garden feature is disabled in IIS 6.0, and the default
number
of worker processes assigned to an application pool is set to 1. You can
enable Web garden functionality by setting the number of worker processes
assigned to an
application pool to a number greater than 1. For information about how to
configure a Web garden by using IIS Manager, see

#Configure Application Pool Performance
http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Lib...
d3f37b8-c7ef-4a9c-9b78-eba9412181d8.mspx

To verify this, you could print out the process id of the request by:

Response.Write(System.Diagnostics.Process.GetCurrentProcess().Id);

Regards,
Walter Wang (wawang@online.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.