开发者

ASP.net Global Cache?

开发者 https://www.devze.com 2023-03-31 14:59 出处:网络
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 data

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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号