How to properly use IMemoryCache?
Hi, all:
I'm working on improving latency for a Razor Page page load, which I can immediately do by caching query results. I've read this: https://www.learnrazorpages.com/razor-pages/caching, but I don't entirely understand how to:
My main questions with this:
Passing that as the last parameter of the
I'm working on improving latency for a Razor Page page load, which I can immediately do by caching query results. I've read this: https://www.learnrazorpages.com/razor-pages/caching, but I don't entirely understand how to:
- Manage the lifetime of the cache disposable
- Asynchronously get or create an entry
My main questions with this:
- I didn't use a
usingstatement like the page instructs with managing cache entries, is this okay? - The compiler is warning me of a possible null reference assignment, but how can
GetOrCreateAsyncreturn null if myGetProblematicIndicatorDisplaysAsyncwill never return null?
var options = new MemoryCacheEntryOptions().SetAbsoluteExpiration(DateTime.Now.AddMinutes(10));Passing that as the last parameter of the
GetorCreateAsync call (also, should options become a field of my class?). Any advice or warnings about using the IMemoryCache would be appreciated, thanks!