Russell Steen
Russell Steen

Reputation: 6612

ASP.net Global Cache?

Is there a way in ASP.net to cache something to memory say x="foo", such that any session can access that cache? Due to contraints, I have to use memory and can't store this in a database or file system. It's fairly volatile and does not need to hang around for a long time.

Upvotes: 1

Views: 5274

Answers (2)

Claudio Redi
Claudio Redi

Reputation: 68440

You can use this piece of code to store on the Cache some data for 30 minutes (absolute expiration)

Cache.Insert("CacheItemKey", "CachedItemValue",  null, 
    DateTime.Now.AddMinutes(30),   System.Web.Caching.Cache.NoSlidingExpiration);

On this MSDN article you have a lot of information about how to use the Cache object

If you're not using a web farm or a web garden the cache object object is global for ALL your web sessions.

Upvotes: 4

Chris Shaffer
Chris Shaffer

Reputation: 32585

Sounds like the HttpContext.Cache would work well for that. Alternatively, I've never used it but HttpContext.ApplicationInstance won't get cleared out by the system (Cache will occasionally be cleaned outside of your control).

Upvotes: 1

Related Questions