Skip to main content

Provides a decorator for caching a function and an equivalent command-line util.

Project description

Provides a decorator for caching a function and an equivalent command-line util.

It wraps an ordinary function. Whenever the function is called with the same arguments, the result is loaded from the cache instead of computed.

Quickstart

$ pip install charmonium.cache
>>> import charmonium.cache as ch_cache
>>> @ch_cache.decor(ch_cache.MemoryStore.create())
... def square(x):
...     print('computing')
...     return x**2
...
>>> square(4)
computing
16
>>> square(4) # square is not called again; answer is just looked up
16

Customization

cache_decor is flexible because it supports multiple backends.

  1. MemoryStore: backed in RAM for the duration of the program (see example above).

  2. FileStore: backed in a file which is loaded on first call.

>>> import charmonium.cache as ch_cache
>>> @ch_cache.decor(ch_cache.FileStore.create("tmp/1"))
... def square(x):
...     return x**2
...
>>> # Now that we cache in a file, this is persistent between runs
>>> # So I must clear it here.
>>> square.clear()
>>> list(map(square, range(5)))
[0, 1, 4, 9, 16]
>>> import os
>>> os.listdir("tmp/1")
['__main__.square_cache.pickle']
  1. DirectoryStore: backed in a directory. Results are stored as individual files in that directory, and they are loaded lazily. Use this for functions that return large objects.

    >>> import charmonium.cache as ch_cache
    >>> @ch_cache.decor(ch_cache.DirectoryStore.create("tmp/2"))
    ... def square(x):
    ...     return x**2
    ...
    >>> # Now that we cache in a file, this is persistent between runs
    >>> # So I must clear it here.
    >>> square.clear()
    >>> list(map(square, range(5)))
    [0, 1, 4, 9, 16]
    >>> import os
    >>> sorted(os.listdir("tmp/2/__main__.square"))
    ['(0).pickle', '(1).pickle', '(2).pickle', '(3).pickle', '(4).pickle']
    
  2. Custom stores: to create a custom store, just extend ObjectStore and implement a dict-like interface.

FileStore and DirectoryStore can both themselves be customized by:

  • Providing a cache_path (conforming to the PathLike interface), e.g. one can transparently cache in AWS S3 with an S3Path] object.

cache_decor also takes a “state function” which computes the value of some external state that this computation should depend on. Unlike the arguments (which the cache explicitly depends on), values computed with a different state are evicted out, so this is appropriate when you never expect to revisit a prior state (e.g. modtime of a file could be a state, as in make_file_state_fn).

With verbose=True, this will output to a logger.

>>> import charmonium.cache as ch_cache
>>> @ch_cache.decor(ch_cache.MemoryStore.create(), verbose=True)
... def square(x):
...     print('computing')
...     return x**2
...
>>> square(4) # doctest:+SKIP
2020-06-19 11:31:40,197 - __main__.square: miss with args: (4,), {}
computing
16
>>> square(4) # doctest:+SKIP
2020-06-19 11:31:40,197 - __main__.square: hit with args: (4,), {}
16

CLI

# cache a commandline function based on its args
$ cache --verbose -- compute_square 6
miss for square(["6"])
36

$ cache -- compute_square 6
hit for square(["6"])
36

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

charmonium.cache-0.5.0.tar.gz (37.2 kB view details)

Uploaded Source

Built Distribution

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

charmonium.cache-0.5.0-py3-none-any.whl (37.6 kB view details)

Uploaded Python 3

File details

Details for the file charmonium.cache-0.5.0.tar.gz.

File metadata

  • Download URL: charmonium.cache-0.5.0.tar.gz
  • Upload date:
  • Size: 37.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.8 CPython/3.8.3 Linux/5.6.0-2-amd64

File hashes

Hashes for charmonium.cache-0.5.0.tar.gz
Algorithm Hash digest
SHA256 1e4850be93646b35bd26b4516527533db1758dbdf8630cb2bc0f934ec5c67cdd
MD5 5728c7022454d73d429aa005749741e0
BLAKE2b-256 e6f66e1b3876d407c79373acc87fb5bdae4c13729dda0dd603cc6d58c89c4c9b

See more details on using hashes here.

File details

Details for the file charmonium.cache-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: charmonium.cache-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 37.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.8 CPython/3.8.3 Linux/5.6.0-2-amd64

File hashes

Hashes for charmonium.cache-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b36b643120856a6b18fc064f537f8e5756fdc5c3fc69d03f4b5ba7be43ef6c50
MD5 ee3ea874b06103bd471ec370ee9126e9
BLAKE2b-256 b0e3eb41fdc30c00117ce6156b7685f081f75568e52e0b02960ddd0a1e264603

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