Implement a caching mechanism to minimize the number of HTTP requests sent by the client #200
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In a project of mine, I end up doing a lot of requests to the public Musicbrainz API endpoint. Due to the throttle policies in place (or the heavy load that sometimes occur on this public instance), some of my requests end up raising a 503 error.
To partially adress that, I've implemented a basic cache mechanism, to store API results and return the API results from the cache when possible, effectively minimizing the number of HTTP requests made.
I'll let you have a look at the commits for more details, but the implementation is a follows:
_mb_request
function will first try to retrieve the value from the cacheThis implementation is agnostic regarding how the cache behave. A simple cache (
DictCache
) is provided to demonstrate the cache behaviour, but more complex caches (using real cache systems, such as Redis or Memcached) are totally doable.The cache key for each response is built using the URL and arguments (such as includes), so there should not be any collision.
The build may fail on Travis, because the tests I wrote require the
mock
library. I've added it to the tox dependencies, but since it's not called on Travis, I'm not sure how to make the build install the library before running tests.Documentation is attached in the commit, if you want more details.
Any feedback is welcome, there are probably a few things to fix before this can be merged :)