KDon
KDon

Reputation: 53

How to write asp.net code that adds collection of objects to Caching that expires after 60 minutes?

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

Answers (1)

GHP
GHP

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

Related Questions