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-refresh-session.

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.1.tar.gz (13.7 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.1-py3-none-any.whl (17.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: boto3_client_cache-0.1.1.tar.gz
  • Upload date:
  • Size: 13.7 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.1.tar.gz
Algorithm Hash digest
SHA256 27b0c48e8445f02fbd9d98f63f792afbf68cc75f69a988e83fe9ca16d23b3c4f
MD5 819fa1f42d7b5dfde89bfc307021b4d2
BLAKE2b-256 835e2afaccf30b52f44972ad970da7bc13b4c05fe254fc4928c3005ab178b256

See more details on using hashes here.

File details

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

File metadata

  • Download URL: boto3_client_cache-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 17.1 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fef53bf84ad4709cd58bf6b7e71890135261193536b50288abc72a268c826baa
MD5 74d0c2e6cbe8f776b177695f5944033f
BLAKE2b-256 a016b42a7eb557343d7dabcc154ad70c13013dcb32349b94e396821dc3faca5f

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