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
>>> with square.disabled():
...     # disable caching; always recomptue
...     square(4)
...
computing
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']
  1. 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.6.0.tar.gz (37.5 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.6.0-py3-none-any.whl (37.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: charmonium.cache-0.6.0.tar.gz
  • Upload date:
  • Size: 37.5 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.6.0.tar.gz
Algorithm Hash digest
SHA256 f07ac0b1e6d3b759ba01dee3549a7fe01cb1f6bd4ceb6cddc203abc8cdf022a1
MD5 d4d9007aa48169a071422f4f89a3a693
BLAKE2b-256 06e575b77746640aad424cc54ad5b3e024274d232bef63c0eafc116b55d5b790

See more details on using hashes here.

File details

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

File metadata

  • Download URL: charmonium.cache-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 37.7 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.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a55f30873dc009545444213577d357d01d392860c3d8b48e491e0a72b1c71c25
MD5 ae34c660c1dadcdaa59509e9d93a35e3
BLAKE2b-256 b551e25b658cd77788fd68c13f32cd0791bd81e5f0c71925506a2325a1bde767

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