Reputation: 53
I want to write asp.net code that adds collection of objects named colSatates to Caching that expires after 60 minutes? What is the correct way to adds the list to cashing to expire in 60 minutes?
Upvotes: 0
Views: 90
Reputation: 3231
var expire = DateTime.Now.AddHours(1);
var colSatatesObj = { ... }; // your object here
HttpContext.Current.Cache.Insert("colSatates", colSatatesObj, null, expire, Cache.NoSlidingExpiration);
The above code packs the colSatatesObj object into the Application Cache for 1 hour, after which it will return null
when retrieved via the key name of "colSatates".
To retrieve it:
var colSatatesObj = HttpContext.Current.Cache["colSatates"] as ColSatateClassName; // whatever your class is called.
if (colSatatesObj != null) {
// do stuff with colSatatesObj
}
Upvotes: 1