ASP.net Global Cache?
Is there a way in ASP.net to cache something to memory say x="foo", such that any session can acce开发者_开发百科ss 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.
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.
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).
精彩评论