Cache object expiration

G

Guest

Hello All,

I inserted an object into cache with absolute expiration using the following
code:

DateTime AbsoluteExpirationDate = DateRangesForDataLists[0,0].AddDays(1);

//Expires the next day
Cache.Insert("DataListStartEndDates",DateRangesForDataLists,null,AbsoluteExpirationDate,TimeSpan.Zero);

How can I know if the above cache object expired or not before accessing? Is
there any API in cache class that I could use?

Any pointers?

Thank you.
 
G

Guest

the simplest way to test for expiration is to check if the Cache item is null:

if Cache["DataListStartEndDates"]==null)
{
// re-add the item here
}

You can also configure CacheItemRemoved calllback that gets fired when the
item is evicted from the Cache. the documentation at MSDN or in your local
help shows example code.
Peter
 
G

Guest

So if the cache object's expiration has expired it is instantly set to null?

Thank you.

Peter Bromberg said:
the simplest way to test for expiration is to check if the Cache item is null:

if Cache["DataListStartEndDates"]==null)
{
// re-add the item here
}

You can also configure CacheItemRemoved calllback that gets fired when the
item is evicted from the Cache. the documentation at MSDN or in your local
help shows example code.
Peter


--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




Diffident said:
Hello All,

I inserted an object into cache with absolute expiration using the following
code:

DateTime AbsoluteExpirationDate = DateRangesForDataLists[0,0].AddDays(1);

//Expires the next day
Cache.Insert("DataListStartEndDates",DateRangesForDataLists,null,AbsoluteExpirationDate,TimeSpan.Zero);

How can I know if the above cache object expired or not before accessing? Is
there any API in cache class that I could use?

Any pointers?

Thank you.
 
G

Guest

Diffident,
When an object in Cache expires, it is "evicted" - it's no longer there. So
if you look for it by key, you will get null.
Actually testing for null before attempting to access anything in Cache ,
Session, Application or Viewstate is a good programming practice.
Hope that helps.
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




Diffident said:
So if the cache object's expiration has expired it is instantly set to null?

Thank you.

Peter Bromberg said:
the simplest way to test for expiration is to check if the Cache item is null:

if Cache["DataListStartEndDates"]==null)
{
// re-add the item here
}

You can also configure CacheItemRemoved calllback that gets fired when the
item is evicted from the Cache. the documentation at MSDN or in your local
help shows example code.
Peter


--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




Diffident said:
Hello All,

I inserted an object into cache with absolute expiration using the following
code:

DateTime AbsoluteExpirationDate = DateRangesForDataLists[0,0].AddDays(1);

//Expires the next day
Cache.Insert("DataListStartEndDates",DateRangesForDataLists,null,AbsoluteExpirationDate,TimeSpan.Zero);

How can I know if the above cache object expired or not before accessing? Is
there any API in cache class that I could use?

Any pointers?

Thank you.
 
G

Guest

Thank you!! That helped.

Peter Bromberg said:
Diffident,
When an object in Cache expires, it is "evicted" - it's no longer there. So
if you look for it by key, you will get null.
Actually testing for null before attempting to access anything in Cache ,
Session, Application or Viewstate is a good programming practice.
Hope that helps.
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




Diffident said:
So if the cache object's expiration has expired it is instantly set to null?

Thank you.

Peter Bromberg said:
the simplest way to test for expiration is to check if the Cache item is null:

if Cache["DataListStartEndDates"]==null)
{
// re-add the item here
}

You can also configure CacheItemRemoved calllback that gets fired when the
item is evicted from the Cache. the documentation at MSDN or in your local
help shows example code.
Peter


--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




:

Hello All,

I inserted an object into cache with absolute expiration using the following
code:

DateTime AbsoluteExpirationDate = DateRangesForDataLists[0,0].AddDays(1);

//Expires the next day
Cache.Insert("DataListStartEndDates",DateRangesForDataLists,null,AbsoluteExpirationDate,TimeSpan.Zero);

How can I know if the above cache object expired or not before accessing? Is
there any API in cache class that I could use?

Any pointers?

Thank you.
 

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top