Home > Net >  Meet strange things about memoryCache cache, written in the controller is normal, encapsulated into
Meet strange things about memoryCache cache, written in the controller is normal, encapsulated into

Time:05-12

Put the following code in the controller, no problem, but in class, and then call SetAbsolute method in the controller, will go wrong, as the chart, what reason, please tall person to give directions, thank you very much!

 private static IMemoryCache _memoryCache; 


Public CacheHelper (IMemoryCache memoryCache)
{
_memoryCache=memoryCache;
}


///& lt; Summary>
///to create absolutely cache expiration time
///& lt;/summary>
///& lt; Param name="cacheKey & gt;" Cache key
///& lt; Param name="obj" & gt; Cache object & lt;/param>
///& lt; Param name="expireDate & gt;" The expiration time (absolute) minutes & lt;/param>
Public static void SetAbsolute (string cacheKey, object obj, int expireDate=10 * 60)
{
//absolute maturity
Var cacheEntryOptions=new MemoryCacheEntryOptions (). SetAbsoluteExpiration (TimeSpan. FromSeconds (expireDate));

_memoryCache. Set (cacheKey, obj, cacheEntryOptions);
}




CodePudding user response:

Under the null reference, you SetAbsolute is static, you can guarantee the above _memoryCache have any references?
Cachehelper to instantiate,

Controller will succeed because IMemoryCache is dependency injection, of course you can use the

CodePudding user response:

Yes, the controller can succeed because of the dependency injection provides a instance,
And your new class didn't give your class instance, so this error

CodePudding user response:

reference not fat to 130 on the second floor do not change the response of the name:
yes, the controller can succeed because of the dependency injection provides a instance,
And did not give you in your new class instances of classes provide, so at this error
how to do that, build instance, need IMemoryCache memoryCache argument, this I do not know how to

CodePudding user response:

reference 4 floor goodgame365 response:
Quote: refer to the second floor is not fat to 130 do not change the response of the name:
yes, the controller can succeed because of the dependency injection provides a instance,
And did not give you in your new class instances of classes provide, so at this error
how to do that, build instance, need IMemoryCache memoryCache argument, this I do not know how to

If you don't know the dependency injection,
Just build instance, new MemoryCache ()

CodePudding user response:

Not instantiate directly into you,,, the new one is not just a matter of,,,

CodePudding user response:

This new MemoryCache () no, need new MemoryCache (IMemoryCache parameters), but I don't know how to give this parameter?
  •  Tags:  
  • C #
  • Related