memorycache

Using multiple instances of MemoryCache

会有一股神秘感。 提交于 2019-11-27 18:04:52
I'd like to add caching capabilities to my application using the System.Runtime.Caching namespace, and would probably want to use caching in several places and in different contexts. To do so, I want to use several MemoryCache instances. However, I see here that using more than one instance of MemoryCache is discouraged: MemoryCache is not a singleton, but you should create only a few or potentially only one MemoryCache instance and code that caches items should use those instances. How would multiple MemoryCache instances affect my application? I find this kind of weird because it seems to me

MemoryCache AbsoluteExpiration acting strange

浪子不回头ぞ 提交于 2019-11-27 13:05:23
I'm trying to use a MemoryCache in .net 4.5 to keep track of and automatically update various items, but it seems like no matter what I set as an AbsoluteExpiration it will always only expire in 15 seconds or more. I want the cache items to expire every 5 seconds, but it always expires in at least 15 seconds, and if I move the expiration time out, it will end up being something like 15 seconds + my refresh interval, but never less than 15 seconds. Is there some internal timer resolution that I'm not seeing? I looked through a bit of the reflected System.Runtime.Caching.MemoryCache code and

Is MemoryCache.Set() thread-safe?

旧城冷巷雨未停 提交于 2019-11-27 11:31:23
问题 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? 回答1: 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

Where are .NET 4.0 MemoryCache performance counters?

浪尽此生 提交于 2019-11-27 10:50:22
问题 Where are .NET 4.0 MemoryCache performance counters? I am looking for their name and I can't find any. Thank you, 回答1: 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

Locking pattern for proper use of .NET MemoryCache

烂漫一生 提交于 2019-11-27 05:54:44
I assume this code has concurrency issues: const string CacheKey = "CacheKey"; static string GetCachedData() { string expensiveString =null; if (MemoryCache.Default.Contains(CacheKey)) { expensiveString = MemoryCache.Default[CacheKey] as string; } else { CacheItemPolicy cip = new CacheItemPolicy() { AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddMinutes(20)) }; expensiveString = SomeHeavyAndExpensiveCalculation(); MemoryCache.Default.Set(CacheKey, expensiveString, cip); } return expensiveString; } The reason for the concurrency issue is that multiple threads can get a null key and

MemoryCache does not obey memory limits in configuration

百般思念 提交于 2019-11-26 23:30:05
I’m working with the .NET 4.0 MemoryCache class in an application and trying to limit the maximum cache size, but in my tests it does not appear that the cache is actually obeying the limits. I'm using the settings which, according to MSDN , are supposed to limit the cache size: CacheMemoryLimitMegabytes : The maximum memory size, in megabytes, that an instance of an object can grow to." PhysicalMemoryLimitPercentage : "The percentage of physical memory that the cache can use, expressed as an integer value from 1 to 100. The default is zero, which indicates that MemoryCache instances manage

How to clear MemoryCache?

大城市里の小女人 提交于 2019-11-26 19:47:58
I have created a cache using the MemoryCache class. I add some items to it but when I need to reload the cache I want to clear it first. What is the quickest way to do this? Should I loop through all the items and remove them one at a time or is there a better way? Dispose the existing MemoryCache and create a new MemoryCache object. The problem with enumeration The MemoryCache.GetEnumerator() Remarks section warns: "Retrieving an enumerator for a MemoryCache instance is a resource-intensive and blocking operation. Therefore, the enumerator should not be used in production applications." Here

MemoryCache Empty : Returns null after being set

会有一股神秘感。 提交于 2019-11-26 19:37:55
I have a problem with an MVC 3 application that is using the new .NET 4 System.Runtime.Caching MemoryCache. I notice that after a seemingly unpredictable time, it stops caching stuff, and acts like it's empty. Consider this bit of code that I took straight from a test View in ASP.NET MVC: MemoryCache.Default.Set("myname","fred", new CacheItemPolicy() { SlidingExpiration = new TimeSpan(0,5,0) }); Response.Write(MemoryCache.Default["myname"]); When it's working, predictably "fred" gets printed. However, when the problem starts to occur, despite the Set() , the value of MemoryCache.Default[

Locking pattern for proper use of .NET MemoryCache

吃可爱长大的小学妹 提交于 2019-11-26 11:46:54
问题 I assume this code has concurrency issues: const string CacheKey = \"CacheKey\"; static string GetCachedData() { string expensiveString =null; if (MemoryCache.Default.Contains(CacheKey)) { expensiveString = MemoryCache.Default[CacheKey] as string; } else { CacheItemPolicy cip = new CacheItemPolicy() { AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddMinutes(20)) }; expensiveString = SomeHeavyAndExpensiveCalculation(); MemoryCache.Default.Set(CacheKey, expensiveString, cip); } return