-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
adding basic cache middleware, currently no cache size limit. #26
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
Conversation
This is racey no? What is protecting the Entries map? |
Yeah, good catch. I've added a RWMutex to protect the map. |
Hi @thomas4019, Just a friendly FYI. When writing code (like a cache) in Go that you know will be accessed by multiple goroutines, always pass the -race flag Your RWMutex is a good start but you'll soon run into performance issues. If you think about it, imagine 20k requests per second coming into your server and they all need to check with the cache first for an asset. They will all suddenly be vying for that one and only lock. It will slow everything down. What you want to read up on is caching algorithms and consistent hashing. You could also dive into my BadActor project which uses an LRU and the jump consistent hashing algo. I would not use the jump hash algo for this usecase but it will give you some ideas. Feel free to ask any questions you want. Later, |
Ok, you've done a middleware that saves the cache in the memory. |
We're looking into some caching libraries, including that one. Great feedback so far! |
The implementation of @thomas4019 works, I think we can accept and then evolve the caching system (or choose another) |
As a user.Here is some problem. |
@qq08420 Please open a separate issue with your question, and explain what you mean, like what error message you're getting, etc. |
Thanks for all the suggestions and ideas for the cache middleware. Sadly, I won't have time to make any improvements in the near future. If anyone has time there are definitely some improvements that can be made to the header processing and the actual cache implementation. This middleware works as is, but as @jaredfolkins said, the performance could be improved a lot by using an alternative to the plain mutex. Anybody have time and desire to improve this middleware? |
While it technically works, I'm not sure, as is, if this middleware does more good than harm. Once pulled in, it more likely for people to believe that this middleware "works" and then be bitten by multiple issues.
In short, as is, this could cause a lot of headaches and future issues that may not be worth it. It seems wiser to either leave this issue and have this as a WIP branch, rather than merging it and claiming that there's a cache middleware when there really isn't. |
I agree with @nemothekid. I would prefer it remains as a WIP until someone is able to pick it up and finish it. |
@thomas4019 If it's alright with you, I'm going to close the PR since it turns out caches are really hard. One of us will be able to use your code as a starting point, though. Thank you for working on this! |
I'm curious what people think of what I have so far. It's working but doesn't yet delete cache entries once it exceeds the limit.