Skip to main content

A Multiton pattern implementation with TTL-based cache expiry

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

rarg-multiton

A small, self-contained implementation of the Multiton pattern with TTL-based cache expiry.

A Multiton wraps a factory function and its arguments. The underlying instance is created lazily on first access to .instance and cached, keyed on the (frozen, hashable) factory and arguments. Any Multiton constructed with the same factory and arguments shares the cached instance. Multiton objects are themselves hashable, equality-comparable and pickleable (with pickle, cloudpickle and dill), provided their arguments are too.

Cached instances expire after ttl seconds of inactivity; accessing .instance resets the TTL. Expired entries are swept on every access via a min-heap ordered by expiry time.

from rarg_multiton import Multiton


def open_connection(url: str, timeout: float = 1.0) -> Connection:
    ...


# The resource is only created when `.instance` is first accessed.
resource = Multiton(open_connection, "https://www.python.org", timeout=10.0)
response = resource.instance.request("GET", "/foo/bar.html")

# Override the default TTL (300s) per-instance.
resource = Multiton(open_connection, "https://www.python.org").with_args(ttl=60.0)

Installation

pip install rarg-multiton

The package has no required runtime dependencies. numpy is an optional dependency: when installed, numpy.ndarray factory arguments are frozen by their bytes, shape and dtype so they can be used as cache keys.

Development

This package uses uv.

uv sync
uv run --group test py.test tests/
uv run --dev pre-commit run -a

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

rarg_multiton-0.0.1.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

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

rarg_multiton-0.0.1-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file rarg_multiton-0.0.1.tar.gz.

File metadata

  • Download URL: rarg_multiton-0.0.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for rarg_multiton-0.0.1.tar.gz
Algorithm Hash digest
SHA256 31d0b4de55211d3ee4a1671ec978cb102599ee2e33884c6b874824de8a9b4fc2
MD5 3189258e362cbf731dcad5394be00f27
BLAKE2b-256 91c52ec18cb903e44c124daf25f68a2a21f13a10d1379ec856ca4001d5cf8dcd

See more details on using hashes here.

Provenance

The following attestation bundles were made for rarg_multiton-0.0.1.tar.gz:

Publisher: ci.yml on ratt-ru/rarg-multiton

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rarg_multiton-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: rarg_multiton-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for rarg_multiton-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 894dee6a1eafdfc0b525679c07a5188148091f50cab6abd75d4c92afc73771be
MD5 9ca18d914097219575f251cbc0a7826a
BLAKE2b-256 1616d89e7d7c27ef338fd46746cf4f56638a51fff041def5ccd10b649449fb35

See more details on using hashes here.

Provenance

The following attestation bundles were made for rarg_multiton-0.0.1-py3-none-any.whl:

Publisher: ci.yml on ratt-ru/rarg-multiton

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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