Skip to main content

nicecache

A cache that persists data between runs. Trust me, you need this.

@functools.cache is nice, but there's a slight problem -- each program starts its own cache fresh. In comes @nicecache.nicecache. Use it just like you'd use @functools.cache, but rest assured that you really only have to compute this once and they'll persist across runs (by saving to disk!).


Uses diskcache to manage things behind the scenes.

@nicecache decorator

Got some slow data-loading or preprocessing that you'd rather just compute once? Frustrated that functools.cache spawns a new cache each time you re-run your program? You're in the right place.

Here's a really simple example:

from time import time
from nicecache import nicecache
@nicecache(tmp_cache_dir)
def slowtimes(a, b):
    print('saving to cache:', (a, b))
    time.sleep(5)
    return a * b

def test():
    t1 = time()
    print(slowtimes(5, 3))
    t2 = time()
    print(f'Func 1 time: {t2-t1}s')
    print(slowtimes(5, 3))
    t3 = time()
    print(f'Func 2 time: {t3-t2}s')
    print(slowtimes(4, 2))
    t4 = time()
    print(f'Func 3 time: {t4-t3}s')
    print(slowtimes(4, 2))
    t5 = time()
    print(f'Func 4 time: {t5-t4}s')
saving to cache: (5, 3)
15
Func 1 time: 5.0264012813568115
15
Func 2 time: 0.0017404556274414062
saving to cache: (4, 2)
8
Func 3 time: 5.011389970779419
8
Func 4 time: 0.0015897750854492188

Specifying a cache path

By default values are cached to ~/.nicecache. Specify a different folder path by passing an argument to the decorator. For example, @nicecache('/tmp/cache/path').

NiceCache

Metadata

Release files for nicecache 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nicecache 0.0.1
File Size Uploaded
nicecache-0.0.1.tar.gz 3.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nicecache 0.0.1
File Interpreter ABI Platform
nicecache-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 6.0 kB

Release files / nicecache-0.0.1.tar.gz

Download URL nicecache-0.0.1.tar.gz
Size 3.0 kB
Tags Source
SHA-256 checksum
How to use checksums
f8a281ba7da24436c51e57514b11383ab5078bbd25351f4ea920a7bce5bd4c42
BLAKE2b-256 checksum
How to use checksums
e8adaf34fd575539f7ec3fab76f57a57f9cd0d930c5939027eafca15f1374ff7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3

Release files / nicecache-0.0.1-py3-none-any.whl

Download URL nicecache-0.0.1-py3-none-any.whl
Size 3.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4ebc40dfd203bbdf180cf934a73d7b7edd94b33222a9ace43ef692406c8470af
BLAKE2b-256 checksum
How to use checksums
b6bb74ccdd4dd36e6543311a4018447225bb399bd7c69aac8ccbd4a303d7debd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page