LazyCache icon indicating copy to clipboard operation
LazyCache copied to clipboard

Policy should be supplied when refreshing the cache due to type mismatch

Open ajbeaven opened this issue 4 years ago • 2 comments

I have been struggling to get LazyCache to behave when setting a SizeLimit in the underlying MemoryCache. Intermittently it seemed, it would throw the following exception despite me supplying a size with the cache entry:

InvalidOperationException: Cache entry must specify a value for Size when SizeLimit is set.

Looking at the code, it appears in the situation where you have a type mis-match, the cache is refreshed however the supplied policy is not passed through which was causing the exception to be thrown.

I'm somewhat surprised no one has faced this before so I'm 70% sure I'm doing something wrong here. Consider this a very tentative PR ;)

ajbeaven avatar Feb 23 '21 03:02 ajbeaven

Thanks for this - looks like a genuine bug! Any chance you could add a unit tests that reproduces the issue so we don't make this mistake again?

alastairtree avatar May 10 '21 20:05 alastairtree

I've added a couple of tests that should do the job. It doesn't test that associated extension methods use IAppCache.GetOrAdd<T>() (the method I cover in my tests) internally, but I assume this is covered elsewhere.

It'd look cleaner to use the GetOrAdd<T>() extension method that takes a TimeSpan, but it's probably safer to test this way. Let me know if you prefer aesthetics though, and I'll change it :)

ajbeaven avatar May 10 '21 23:05 ajbeaven