Skip to main content

Botocache

Caching layer for Boto / Botocore libraries.

Background


This project was started to solve the issue raised here.

My day job requires me to write and use multiple tools and standalone scripts to audit AWS environments. Most of these tools are hacky and written in python which uses boto3 / botocore in the backend to interact with AWS API.

Sometimes, I would have to write a wrapper to combine these scripts to get a custom consolidated report. Since these scripts are standalone, they repeat the same API calls that a previous script would have already called before, leading to redundant API calls, throttling and unnecessary IO wait.

These wrapped tools are sometimes even used as Lambda for automation of certain things. The IO wait times becomes a bottleneck when used in environment such as Lambda where execution is a time-bound activity.

Hence I was looking for a caching layer over boto that can solve reducing these direct redundant calls and the wait times. Thus the birth of botocache.

About


Botocache caches the response of API calls initiated through boto3 / botocore. Any subsequent redundant call will end up getting the previously cached response from Botocache as long as the call is within the expiry timeout of the cached response.

Botocache can work with any cache library that is based on cachetools

It uses the unittest module's patch as the magic component to achieve this. 😉

This project is little hacky given the nature how it achieves caching, but it gets the job done.

Installation


Stable release installation (PyPI) :-

pip3 install botocache

Test release installation (Test PyPI)

pip install -i https://test.pypi.org/pypi/ --extra-index-url https://pypi.org/simple botocache

Installation directly from this Repository:-

pip3 install git+https://github.com/rams3sh/botocache.git

Usage


Below snippet demonstrates usage of botocache.

from boto3.session import Session
from cachetools_ext.fs import FSLRUCache

from botocache.botocache import botocache_context

cache = FSLRUCache(ttl=900, path=".cache", maxsize=1000)

# action_regex_to_cache parameter consists list of regex to be matched against a given action for considering the call to be cached
with botocache_context(cache=cache,
                       action_regex_to_cache=["List.*", "Get.*", "Describe.*"],  
                       call_log=True, # This helps in logging all calls made to AWS. Useful while debugging. Default value is False.
                       supress_warning_message=False # This supresses warning messages encountered while caching. Default value is False. 
                       ):
  cached_session = Session()
  cached_client = cached_session.client('iam')
  paginator = cached_client.get_paginator('list_users')
  for page in paginator.paginate():
    print(page)

"""
Don't do this, if you want to have a new session without caching. 
The below paginator object was initialised under the botocache context which means it's subsequent 
attributes was initialised with patched Botocache class leading it to still use the backend cache. 
"""
for page in paginator.paginate():
  print(page)

"""
Always use a fresh initialised session client and subsequent new objects outside the context of botocache to use 
boto3 without caching layer. Below is an example. 
"""

non_cached_session = Session()
non_cached_client = non_cached_session.client('iam')
paginator = non_cached_client.get_paginator('list_users')
for page in paginator.paginate():
  print(page)

Note:

Botocache has been tested with cachetools_ext's FSLRU cache, but it should logically work with any cache that is compatible with cachetools.

Disclaimer(s)


  • This project was created mainly to support my specific internal use cases. Hence, there is a good scope of it having bugs and functional issues. Feel free to raise a PR / Issue in those cases.

  • Botocache does not understand HTTP related caching specification. It works based on function caching.Its a very simple dumb caching layer that checks for specific attributes in an API call, converts into a key and stores the response against the key. Any subsequent call having matching attributes will be returned with the value stored against the same key.

Metadata

Release files for botocache 0.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for botocache 0.0.6
File Size Uploaded
botocache-0.0.6.tar.gz 3.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for botocache 0.0.6
File Interpreter ABI Platform
botocache-0.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 7.5 kB

Release files / botocache-0.0.6.tar.gz

Download URL botocache-0.0.6.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
657752e86d36cb1bd4b9197a25800bb716029623ec1c778dea542443de5b6758
BLAKE2b-256 checksum
How to use checksums
17acd1093531a52a6c227b132ab3923dc6351cf5de3117e98720c2261a01043e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.2

Release files / botocache-0.0.6-py3-none-any.whl

Download URL botocache-0.0.6-py3-none-any.whl
Size 4.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a8ca51dca7bb5e2319cdc9095ef0f49dbb30a028d1c335471ca696c161f0e0e4
BLAKE2b-256 checksum
How to use checksums
f8c4e140fd6dac246d81861695b46be056ee09747fdec65da23b81ac77c5a25b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.2

Release history Release notifications | RSS feed

This release

0.0.6 This release

2 release files

0.0.4

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page