-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Support caching of API call response based on set time expiry as part of boto3.session.Session #2723
Comments
Until this issue is triaged / accepted for development. I found an interim solution using python unittest.mock 's patch. Below a sample snippet. I have used memoization, but however one may use other custom caching mechanisms for the same. from unittest.mock import patch
from boto3.session import Session
from botocore.client import BaseClient
import memoization
import time
class ResponseCachingProxyClient(BaseClient):
def __init__(self, cached_enabled=True, *args, **kwargs, ):
super().__init__(*args, **kwargs)
self.cache_enabled = cached_enabled
def _make_api_call(self, *args, **kwargs):
action = args[0]
call_verbs_to_cache = ["List", "Get", "Describe"]
if self.cache_enabled and any([action.startswith(call) for call in call_verbs_to_cache]):
return self._make_cached_api_call(*args, **kwargs)
return super()._make_api_call(*args, **kwargs)
@memoization.cached(ttl=900, thread_safe=True, order_independent=True)
def _make_cached_api_call(self, *args, **kwargs):
return super()._make_api_call(*args, **kwargs)
with patch('botocore.client.BaseClient', new=ResponseCachingProxyClient):
session = Session()
client = session.client('iam')
paginator = client.get_paginator('list_users')
start = time.time()
for page in paginator.paginate():
print(page['ResponseMetadata']['HTTPHeaders']['x-amzn-requestid']) # Request Id to track if cache is being used
print("Time taken before caching:", time.time()-start)
start = time.time()
for page in paginator.paginate():
print(page['ResponseMetadata']['HTTPHeaders']['x-amzn-requestid']) # Request Id to track if cache is being used
print("Time taken after caching:", time.time()-start)
# Futher Business logic ..... Thought of posting it here, just in case if anyone else is looking for an interim solution for this problem. |
I created a new project to solve this issue called botocache. |
Hi all, see the comment regarding closure of this issue here:
|
|
Is your feature request related to a problem? Please describe.
I have a scheduled trigger based serverless app running from a central account running on lambda(s) which has to assume role across around 1k accounts and carry out certain actions. Each task within the app runs as separate process and may have to repeat some boto3 api calls within same account. This results in same api calls being called multiple times resulting in IO wait / throttling (which involves sleeping to initiate the next call) increasing the execution time which does sometime lead to lambda timeouts. Writing a function cache wrapper for each boto3 method didn't feel as a scalable solution for me.
This feature request proposes to have a setting where one could set a cache directory with cache expiry timeout. Boto3 package can use that path to store all cached response (something like picked files). Whenever an api call is initiated through a session object , the cache will be checked and the response will be returned if the last cached response is within the cache expiry from the cache file.
The scope of cache would be limited only to that given session.
Describe the solution you'd like
A sample code snippet to give idea about the expected solution is given below :-
The text was updated successfully, but these errors were encountered: