Skip to main content

A simple Python package with a cache for boto3 clients.

Project description

boto3-client-cache


Description

A simple Python package which caches boto3 clients.

Includes LRU eviction. LFU eviction will be included in a future release.

Raison d'Être

boto3 clients consume a large amount of memory. Many developers never notice. At scale, however, this becomes painfully obvious. There is a clear incentive, therefore, to avoid initializing duplicate client objects. Client caching is an obvious solution.

The most challenging aspect of boto3 client caching is selecting robust and standardized unique keys. Managing ad-hoc keys at scale is unwieldy and insecure. boto3-client-cache hashes according to client signatures. Setting and retrieving clients from the client cache therefore requires an explicit declaration of intention -- that is, the developer must explicitly pass client initialization parameters to a CacheKey object in order to set or retrieve a client.

From a developer experience perspective, this design - that is, forcing CacheKey - may feel clunky; however, it ensures setting and retrieving clients are unambiguous operations. Further, locking the client cache, as boto3-client-cache does, prevents race conditions, enabling developers to confidently employ the client cache at scale.

Although boto3-client-cache can help any developer working with the AWS Python SDK at any scale, it was designed primarily for security, cloud, machine learning, and platform teams operating at scale.

boto3-client-cache, it should be noted, is also a critical dependency for boto3-client-cache.

Installation

pip install boto3-client-cache

Quickstart

from boto3_client_cache import ClientCache, ClientCacheKey
import boto3

# create an LRU client cache with a maximum size of 30
cache = ClientCache(max_size=30)

# store boto3 client params in an object
kwargs = {"service_name": "s3", "region_name": "us-west-2"}

# create a cache key using those params
key = ClientCacheKey(**kwargs)

# make the assignment
cache[key] = boto3.client(**kwargs)

# and retrieve the client using the key
s3_client = cache[key]

# this raises a ClientCacheExistsError
cache[key] = boto3.client(**kwargs)

# this raises a ClientCacheNotFoundError
cache[ClientCacheKey(service_name="ec2", region_name="us-west-2")]

# but this returns None instead of raising ClientCacheNotFoundError
cache.get(ClientCacheKey(service_name="ec2", region_name="us-west-2"))

# this raises a ClientCacheError
cache["this is not a ClientCacheKey"]

# and this raises a ClientCacheError
cache[ClientCacheKey("s3")] = "this is not a boto3 client"

License

boto3-client-cache is licensed by the Apache Software License (2.0).

Special Thanks

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

boto3_client_cache-0.1.0.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

boto3_client_cache-0.1.0-py3-none-any.whl (16.9 kB view details)

Uploaded Python 3

File details

Details for the file boto3_client_cache-0.1.0.tar.gz.

File metadata

  • Download URL: boto3_client_cache-0.1.0.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for boto3_client_cache-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9ccdda234a020044116fe0caf487820071844c87a1b835e61a6e317977df5034
MD5 068cbe10d7ef438d4755ea266dcac8f5
BLAKE2b-256 7ed0b5a737bcacd632762a02c0963523ef321ca9a68c0eb43e2a7062b0e9fce9

See more details on using hashes here.

File details

Details for the file boto3_client_cache-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: boto3_client_cache-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 16.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for boto3_client_cache-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 681c565fa867b5e82a2df035a07dcdb443e347c540f1d95cc8a4ad161c2b134e
MD5 1cea108f6b65196d682adf6aaa2c45ee
BLAKE2b-256 7ac8969d2b42587d921499182907164473d280275b305c8ef35f2dee51793eff

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page