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; }
_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