memorycache

How can I detach the object reference on MemoryCache

限于喜欢 提交于 2019-11-30 01:37:14
问题 I'm currently trying out the new MemoryCache in .Net 4 to cache a few bits of data in one of our apps. The trouble I have is the objects are updated and the cache appears to be persisting the changes e.g. public IEnumerable<SomeObject> GetFromDatabase(){ const string _cacheKeyGetDisplayTree = "SomeKey"; ObjectCache _cache = MemoryCache.Default; var objectInCache = _cache.Get(_cacheKeyGetDisplayTree) as IEnumerable<SomeObject>; if (objectInCache != null) return objectInCache.ToList(); // Do

.Net 4 Memory Cache class and user Session

岁酱吖の 提交于 2019-11-29 23:19:09
问题 The new class MemoryCache in .Net 4.0 appears to act just like asp.net caching. My questions are: Is MemoryCache equivalent to storing an object/value in for a user in Session Cache, but not in the code behind of an aspx page. Can a value stored in MemoryCache, which exists on the server, be accessable to a web page event? 回答1: Is MemoryCache equivalent to storing an object/value in for a user in Session Cache No, it is not equivalent. The ASP.NET Session object is per user key/value storage,

MemoryCache Thread Safety, Is Locking Necessary?

喜你入骨 提交于 2019-11-29 21:11:44
For starters let me just throw it out there that I know the code below is not thread safe (correction: might be). What I am struggling with is finding an implementation that is and one that I can actually get to fail under test. I am refactoring a large WCF project right now that needs some (mostly) static data cached and its populated from a SQL database. It needs to expire and "refresh" at least once a day which is why I am using MemoryCache. I know that the code below should not be thread safe but I cannot get it to fail under heavy load and to complicate matters a google search shows

Is MemoryCache scope session or application wide?

喜你入骨 提交于 2019-11-29 20:20:30
I'm using MemoryCache in ASP.NET and it is working well. I have an object that is cached for an hour to prevent fresh pulls of data from the repository. I can see the caching working in debug, but also once deployed to the server, after the 1st call is made and the object is cached subsequent calls are about 1/5 of the time. However I'm noticing that each new client call (still inside that 1 hour window - in fact just a minute or 2 later) seems to have the 1st call to my service (that is doing the caching) taking almost as long as the original call before the data was cached. This made me

Is MemoryCache.Set() thread-safe?

为君一笑 提交于 2019-11-28 18:37:04
The MSDN documentation for MemoryCache.Set unfortunately doesn’t state explicitly whether it is thread-safe or not. Is it safe to use .Get() and .Set() from several threads without an explicit lock? Yes, the MemoryCache class is thread safe : System.Runtime.Caching.MemoryCache is threadsafe. Multiple concurrent threads can read and write a MemoryCache instance. Internally thread-safety is automatically handled to ensure the cache is updated in a consistent manner. What this might be referring to is that data stored within the cache may itself not be threadsafe. For example if a List is placed

Where are .NET 4.0 MemoryCache performance counters?

风流意气都作罢 提交于 2019-11-28 17:52:36
Where are .NET 4.0 MemoryCache performance counters? I am looking for their name and I can't find any. Thank you, That's a loaded question with a very long answer. I doubt it is going to be helpful, let's talk about the real problem you are trying to solve. Those performance counters have to be registered first before you can see them. Start an elevated console prompt (right-click the shortcut and use Run as Administrator) and type these commands: cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 lodctr netmemorycache.ini That adds the required registry entries so the MemoryCache can create

Is MemoryCache scope session or application wide?

倾然丶 夕夏残阳落幕 提交于 2019-11-28 15:46:39
问题 I'm using MemoryCache in ASP.NET and it is working well. I have an object that is cached for an hour to prevent fresh pulls of data from the repository. I can see the caching working in debug, but also once deployed to the server, after the 1st call is made and the object is cached subsequent calls are about 1/5 of the time. However I'm noticing that each new client call (still inside that 1 hour window - in fact just a minute or 2 later) seems to have the 1st call to my service (that is

How to deal with costly building operations using MemoryCache?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 15:20:54
On an ASP.NET MVC project we have several instances of data that requires good amount of resources and time to build. We want to cache them. MemoryCache provides certain level of thread-safety but not enough to avoid running multiple instances of building code in parallel. Here is an example: var data = cache["key"]; if(data == null) { data = buildDataUsingGoodAmountOfResources(); cache["key"] = data; } As you can see on a busy website hundreds of threads could go inside the if statement simultaneously until the data is built and make the building operation even slower, unnecessarily consuming

How can I use Dependency Injection in a .Net Core ActionFilterAttribute?

≯℡__Kan透↙ 提交于 2019-11-28 12:44:41
AuthenticationRequiredAttribute Class public class AuthenticationRequiredAttribute : ActionFilterAttribute { ILoginTokenKeyApi _loginTokenKeyApi; IMemoryCache _memoryCache; public AuthenticationRequiredAttribute(IMemoryCache memoryCache) { _memoryCache = memoryCache; _loginTokenKeyApi = new LoginTokenKeyController(new UnitOfWork()); } public override void OnActionExecuting(ActionExecutingContext filterContext) { var memory = _memoryCache.Get(Constants.KEYNAME_FOR_AUTHENTICATED_PAGES); string requestedPath = filterContext.HttpContext.Request.Path; string tokenKey = filterContext.HttpContext

How to deal with costly building operations using MemoryCache?

喜夏-厌秋 提交于 2019-11-27 19:44:57
问题 On an ASP.NET MVC project we have several instances of data that requires good amount of resources and time to build. We want to cache them. MemoryCache provides certain level of thread-safety but not enough to avoid running multiple instances of building code in parallel. Here is an example: var data = cache["key"]; if(data == null) { data = buildDataUsingGoodAmountOfResources(); cache["key"] = data; } As you can see on a busy website hundreds of threads could go inside the if statement