-
Notifications
You must be signed in to change notification settings - Fork 440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cache entry must specify a value for Size #148
Comments
Other libraries have this issue as well, I don't think we can ever fix this: MiniProfiler/dotnet#501 |
@cristipufu certainly options can be added to workaround it though |
I'm having this issue too. What is the proposed workaround here? |
Just ran into this issue too. @thomasgalliker Right now I'm just removing the size limit and setting shorter expiration times for my service's items. That way the part of the cache I directly control shouldn't get too big, though there will be some trial and error to set it properly. I agree with @onionhammer that having this as an optional setting would be a nice workaround. It is unfortunate the default MemoryCache isn't more flexible. |
Setting the SizeLimit to null |
I just don't set it at all there: |
That's now really weird. It works well with When I implement my own MemoryCacheRateLimitCounterStore and replace the MemoryCacheEntryOptions object, the rate limiting also works when run from unit tests:
I'm not sure now who's the culprit in this game... |
If you're using EF Core, you'll need to add memory cache with sizelimit of null before initializing EF Core or another dependency that initializes memorycache This issue should be reopened IMO. |
Thanks for your help guys. I'll definitely give it another try tomorrow. |
What about this: In ConfigureServices I no longer call AddMemoryCache() but I register singleton services for CustomMemoryCacheIpPolicyStore and CustomMemoryCacheRateLimitCounterStore. Those two Custom* services create their own instance of IMemoryCache - so, they no longer depend on MemoryCacheOptions made by other consumers of IMemoryCache (just like EFCore).
CustomMemoryCacheRateLimitCounterStore inherits MemoryCacheRateLimitCounterStore but instead of injecting IMemoryCache, we create an instance of IMemoryCache using MemoryCacheHelper.CreateMemoryCache(...).
Same goes for CustomMemoryCacheIpPolicyStore:
And here we have the implementation of MemoryCacheHelper. MemoryCacheOptions uses a SizeLimit = null.
I kindly ask for your opinion. Is this a valid solution or a cheap hack? Is there another way to achieve the same goal (having an independent IMemoryCache instance)? |
@thomasgalliker I think your solution is a reasonable workaround! I will note that it looks like you're creating two separate instances of the memory cache, which might cause some issues. I would suggest updating your static class to store the cache ones it's created, then just return the stored cache instead of creating new ones each time. |
I think the best solution here is also a fairly easy one - Use an internal serviceprovider (like EF Core does by default). |
@onionhammer so basically you're suggesting that AspNetCoreRateLimit should use a new instance of MemoryCache (instead of the default one)? |
@cristipufu yes, exactly; a separate MemoryCache (stored in a separate ServiceProvider instance) without depending on the end-developer to set up a memorycache without a SizeLimit. |
This should be a fairly easy one to knock out, but I'm not seeing a way to set the 'Size()' of an item for the MemoryCacheRateLimitCounterStore and other MemoryCache* providers, for when memory cache's SizeLimit is set.
I think "Size = 1" can be added here:
AspNetCoreRateLimit/src/AspNetCoreRateLimit/Store/MemoryCacheRateLimitStore.cs
Line 43 in c5a4f28
The text was updated successfully, but these errors were encountered: