Skip to content
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-aware functionality #12

Open
HittingSmoke opened this issue Oct 3, 2017 · 2 comments
Open

Cache-aware functionality #12

HittingSmoke opened this issue Oct 3, 2017 · 2 comments

Comments

@HittingSmoke
Copy link

HittingSmoke commented Oct 3, 2017

Since some API calls can be cached, it would be nice if ratelimit could somehow be made aware of the cache.

For example, with the very simple to use requests_cache library you can cache all responses made by the requests library by default.

If your request is r = requests.get('http://example.com/api') you can check r.from_cache which will return True if the response was read from cache.

It would be awesome if I could easily make ratelimit aware of the last time an actual API call was made using r.from_cache in my function so it would only rate limit actual API calls. Something like this:

@ratelimit(1, 10)
def apiCall():
    r = requests.get('http://example.com/api')
    if r.from_cache:
        ratelimit.skip()

Would this be terribly difficult to implement or is there already a way to do this that isn't documented in the readme? The hook method doesn't account for time the rest of the script is running between API requests so it wastes a lot of time.

@rokcarl
Copy link

rokcarl commented Jul 12, 2018

Can't you have a cache decorator wrap around what you have? So the cache decorator would be hit first and it would return the results if there's something in the cache. If not, the rate limit decorator would be hit that would check if there have been to many API calls.

I haven't tried it, but I'm using Django's cache_memoize and I think it could work like this:

@cache_memoize(3600)
@ratelimit(1, 10)
def apiCall():
    return requests.get('http://example.com/api').text

@tomasbasham
Copy link
Owner

If that works that would be awesome. I'd be happy to add it to the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants