memorycache

Why getting multiple instances of IMemoryCache in ASP.Net Core?

南楼画角 提交于 2019-12-21 09:19:15
问题 I have what I believe is a standard usage of IMemoryCache in my ASP.NET Core application. In startup.cs I have: services.AddMemoryCache(); In my controllers I have: private IMemoryCache memoryCache; public RoleService(IMemoryCache memoryCache) { this.memoryCache = memoryCache; } Yet when I go through debug, I end up with multiple memory caches with different items in each one. I thought memory cache would be a singleton? Updated with code sample: public List<FunctionRole> GetFunctionRoles() {

.net MemoryCache - notify on item removed

余生颓废 提交于 2019-12-21 07:44:13
问题 I'm using a .net Memory Cache with .NET 4.0 and c#, I want my application to be notified when an item is removed (so I can write that it has been removed to a log file or notify the UI, that the item is removed). Is there anyway to do this. I'm using System.Runtime.Caching.MemoryCache not System.Web.Caching 回答1: EDIT : If you're using the System.Runtime.Caching.MemoryCache there is a callback on the CacheItemPolicy object for deletion, as well as one for update. myMemoryCache.Set("key", null,

.net MemoryCache - notify on item removed

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-21 07:41:10
问题 I'm using a .net Memory Cache with .NET 4.0 and c#, I want my application to be notified when an item is removed (so I can write that it has been removed to a log file or notify the UI, that the item is removed). Is there anyway to do this. I'm using System.Runtime.Caching.MemoryCache not System.Web.Caching 回答1: EDIT : If you're using the System.Runtime.Caching.MemoryCache there is a callback on the CacheItemPolicy object for deletion, as well as one for update. myMemoryCache.Set("key", null,

Volatile and cache behaviour

对着背影说爱祢 提交于 2019-12-18 13:38:18
问题 I read post C volatile variables and Cache Memory But i am confused. Question: whether OS will take care itself OR programmer has to write program in such a way that variable should not go into cache as mention like declaring variable as _Uncached. Regards Learner 回答1: To clarify: volatile is a C concept and tells the compiler to fetch a variable each time from memory rather then use a "compiler-generated" cached version in registers or optimise certain code. What may be causing confusion

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

大憨熊 提交于 2019-12-17 20:57:06
问题 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

MemoryCache AbsoluteExpiration acting strange

馋奶兔 提交于 2019-12-17 10:47:56
问题 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

C volatile variables and Cache Memory

人盡茶涼 提交于 2019-12-17 10:19:44
问题 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 that, Volatile keyword tells compiler that the variable references shouldn't be optimized and should be read as programmed in the code. Cache is controlled by cache hardware transparently, hence when processor issues an address, it doesn't know

MemoryCache Empty : Returns null after being set

余生长醉 提交于 2019-12-17 04:47:10
问题 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.

How to clear MemoryCache?

夙愿已清 提交于 2019-12-17 04:23:31
问题 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? 回答1: Dispose the existing MemoryCache and create a new MemoryCache object. 回答2: The problem with enumeration The MemoryCache.GetEnumerator() Remarks section warns: "Retrieving an enumerator for a MemoryCache instance is a resource

MVC Net Core Pass MemoryCache to All Controllers

穿精又带淫゛_ 提交于 2019-12-11 15:45:34
问题 I am building a online shop website. I want to display ProductCategory as a Sidebar (short list categories, Eg Clothes, Electronics, Furniture, Books, etc). At the end, I want to send around productcategory _cache variable to all controllers.Does this methodology seem correct? Additionally, how do I remove unnecessary memorycache parameter in Home Controller? Make a method in Scheduled stuff to retrieve it? DI Solution: public class HomeController : Controller { private readonly IMemoryCache