Questions tagged [memorycache]

The MemoryCache class is a concrete implementation of the abstract ObjectCache class in .Net

The MemoryCache class is a concrete implementation of the abstract ObjectCache class. It is part of the System.Runtime.Caching Namespace.

353 questions
136
votes
10 answers

Locking pattern for proper use of .NET MemoryCache

I assume this code has concurrency issues: const string CacheKey = "CacheKey"; static string GetCachedData() { string expensiveString =null; if (MemoryCache.Default.Contains(CacheKey)) { expensiveString =…
Allan Xu
  • 7,998
  • 11
  • 51
  • 122
123
votes
12 answers

How to clear MemoryCache?

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…
Retrocoder
  • 4,483
  • 11
  • 46
  • 72
121
votes
7 answers

MemoryCache Thread Safety, Is Locking Necessary?

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…
James Legan
  • 1,903
  • 2
  • 14
  • 21
115
votes
1 answer

What's the difference between MemoryCache.Add and MemoryCache.Set?

I read the MSDN documentation but didn't really understand it. I believe that the behavior of Set is "replace existing, or add" (atomically). Is that correct?
Storm
  • 4,307
  • 11
  • 40
  • 57
93
votes
7 answers

MemoryCache does not obey memory limits in configuration

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…
Canacourse
  • 1,805
  • 2
  • 20
  • 37
76
votes
3 answers

Using multiple instances of MemoryCache

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…
Adi Lester
  • 24,731
  • 12
  • 95
  • 110
75
votes
3 answers

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

AuthenticationRequiredAttribute Class public class AuthenticationRequiredAttribute : ActionFilterAttribute { ILoginTokenKeyApi _loginTokenKeyApi; IMemoryCache _memoryCache; public AuthenticationRequiredAttribute(IMemoryCache…
canmustu
  • 2,304
  • 4
  • 21
  • 34
65
votes
6 answers

How to deal with costly building operations using MemoryCache?

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…
Sedat Kapanoglu
  • 46,641
  • 25
  • 114
  • 148
57
votes
2 answers

Is MemoryCache scope session or application wide?

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…
atconway
  • 20,624
  • 30
  • 159
  • 229
53
votes
5 answers

MemoryCache Empty : Returns null after being set

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…
James McCormack
  • 9,217
  • 3
  • 47
  • 57
52
votes
10 answers

How to remove all objects (reset) from IMemoryCache in ASP.NET Core

There is a Remove method to remove an object from IMemoryCache by its key. Is there a way to reset the whole cache and remove all objects? Using the Dispose method as stated by How to clear MemoryCache? does not work: ObjectDisposedException: Cannot…
eadam
  • 23,151
  • 18
  • 48
  • 71
46
votes
4 answers

MemoryCache AbsoluteExpiration acting strange

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…
Jared
  • 1,180
  • 1
  • 9
  • 14
39
votes
7 answers

C volatile variables and Cache Memory

Cache is controlled by cache hardware transparently to processor, so if we use volatile variables in C program, how is it guaranteed that my program reads data each time from the actual memory address specified but not cache. My understanding is…
Microkernel
  • 1,347
  • 4
  • 17
  • 38
36
votes
2 answers

Is MemoryCache.Set() thread-safe?

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?
Timwi
  • 65,159
  • 33
  • 165
  • 230
33
votes
4 answers

Memory Cache in dotnet core

I am trying to write a class to handle Memory cache in a .net core class library. If I use not the core then I could write using System.Runtime.Caching; using System.Collections.Concurrent; namespace n{ public class MyCache { readonly…
MJK
  • 3,434
  • 3
  • 32
  • 55
1
2 3
23 24