ratelimit icon indicating copy to clipboard operation
ratelimit copied to clipboard

Cache-aware functionality

Open HittingSmoke opened this issue 8 years ago • 2 comments

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.

HittingSmoke avatar Oct 03 '17 17:10 HittingSmoke

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

rokcarl avatar Jul 12 '18 08:07 rokcarl

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

tomasbasham avatar Jul 12 '18 09:07 tomasbasham