Skip to main content

A memoize-like decorator that caches to disk.

Project description

diskoize:memoize::disk:memory

What is this

Often when I use @functools.lru_cache I find that the cache is erased between python process restarts. This library is the fix!

import diskoize
import requests

@diskoize.diskoize("scrape_cache.db")
def scrape(url):
  return requests.request(url)


scrape("google.com") # <-- will run only once, even after rerunning this script.

You need to manually delete the cache if you change the function's input-output behavior.

Tests

We use pytest for testing. To run the tests, you can use the following command:

pytest

This will run all the tests in the tests directory. You can also run a specific test file by providing the path to the file:

pytest tests/test_file.py

Development

Make a temporary pip install: pip install -e . Now you can use the library from any python script, and edits to the local diskoize/ folder will be reflected.

Remarks

  • There is a tension between making this library understandable and making it easy to use. -- we always give the option of explicitly naming the backing sqlite database file. -- for autonaming, we currently create or reuse a file in the system temp directory which is based based on the diskoized function's name. It is up to the user to delete invalidated caches. So autonaming should probably only be used for quick-and-dirty experiments where we don't expect to come back to the code after a long while.

TODOs

  • add option in_memory (default False) to store the cache in memory, always with persistent backing.
  • make the tests actually test separate processes accessing the same cache
  • add methods for interacting with the cache
  • add more examples
  • add flush() method, and a lru_cache to support it
  • fix _make_key

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

diskoize-0.1.1.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

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

diskoize-0.1.1-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: diskoize-0.1.1.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for diskoize-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b7e5640a12dc07adc0582e1e01eee046eae0d37189ef067116cd24a98583e525
MD5 2c363c30931bf140aa4dda0d2d5b92d1
BLAKE2b-256 6b3fcd5ebabab0265f453afca7d18ac2c56a4e977633302db1a8ad23b95647f0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: diskoize-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for diskoize-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d0812b230148d49af36b1e6d2a7efb35e03a96f26b7052ebb59d4fe4d8fb7309
MD5 8c8fbb1ba58222ecaaeb3539728708c7
BLAKE2b-256 1dc065b6afbaed6a3973d009091ed2c530e9ef9dcb0aea05342a227d492b37ad

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