Cache any function to disk
Project description
What is this?
The smart way to cache outputs to cold storage.
- auto rebuilds cache when you edit function source code
- uses mutltiprocessing to keep main thread running fast while saving to disk
- excellent change-tracking of arguments thanks to
super_hash - can watch change-tracking of external vars and method attributes
- uses python pickle for saving function outputs, if
dillis available it will use that instead
How do I use this?
pip install cool_cache
from cool_cache import cache, settings
settings.default_folder = None # disable caching to cold-storage, and instead cache to ram
# this is the default, but you can change it
settings.default_folder = "cache.ignore/"
#
#
# simple usage (updates whenever function is edited (excluding comments) or when args change)
#
#
@cache()
def things_with_args(a,b,c):
from time import sleep; sleep(1) # <- simulating a long-running process
return a + b + c
things_with_args(1,2,3) # not yet cached
things_with_args(1,2,3) # uses cache
things_with_args(9,9,9) # not yet cached
things_with_args(9,9,9) # uses cache
#
#
# external vars
#
#
external_counter = 0
@cache(depends_on=lambda:[external_counter])
def things_with_external(a,b,c):
global external_counter
from time import sleep; sleep(1) # <- simulating a long-running process
return external_counter + a + b + c
#
# behavior
#
things_with_external(4,5,6) # not yet cached
things_with_external(4,5,6) # uses cache
external_counter = 1
things_with_external(4,5,6) # not yet cached (because external_counter changed)
things_with_external(4,5,6) # uses cache
#
#
# filepath arguments
#
#
@cache(watch_filepaths=lambda arg1, arg2, arg3: [ arg1, arg2 ]) # because first two args are filepaths
def things_with_files(filepath1, filepath2, c):
with open(filepath1, 'r') as in_file1:
with open(filepath2, 'r') as in_file2:
return in_file1.readlines() + c + in_file2.readlines()
#
# behavior
#
things_with_files("./file1.txt", "./file2.txt", "hello") # not yet cached
things_with_files("./file1.txt", "./file2.txt", "hello") # cached
with open("./file2.txt",'w') as f: f.write(str(" world")) # <-- modify the file
things_with_files("./file1.txt", "./file2.txt", "hello") # not yet cached, because file change is detected
things_with_files("./file1.txt", "./file2.txt", "hello") # cached
#
# cache expiry
#
@cache(keep_for="2d") # bust the cache entry after ~2 days
def sometimes_stale(a, b):
return a + b
# accepted units: m (milliseconds), s, h, d, mo (~30 days), y (~365 days)
@cache(keep_for="500ms") # 500 milliseconds
def quick_refreshing():
...
# set a global default for all caches (can still override per-decorator)
from cool_cache import settings
settings.default_keep_for = "1h"
#
#
# class methods (e.g. self)
#
#
class MyThing:
def __init__(self, path, other_stuff):
self.path = path
self.other_stuff = other_stuff
# for example: self.path changing will affect the cache, but self.other_stuff wont affect the cache
@cache(watch_attributes=lambda self:[ self.path, ])
def do_some_stuff(self, arg1):
from time import sleep; sleep(1)
return self.path + arg1
#
# bust=True wipes out all cached values for this function on the next run
#
@cache(bust=True)
def things(a,b,c):
return 10
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
cool_cache-0.4.0.tar.gz
(2.1 MB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cool_cache-0.4.0.tar.gz.
File metadata
- Download URL: cool_cache-0.4.0.tar.gz
- Upload date:
- Size: 2.1 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9f8c7734956bdaff10278b97e533a239429c7c2638a1315afcb2148c73f3d921
|
|
| MD5 |
9795bd461c211680a4ef61f892b505c3
|
|
| BLAKE2b-256 |
8ae05d95595d85d828944c64255b54ee1f1c642056a4a9fe54806a2bd5770a74
|
File details
Details for the file cool_cache-0.4.0-py3-none-any.whl.
File metadata
- Download URL: cool_cache-0.4.0-py3-none-any.whl
- Upload date:
- Size: 2.3 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
71fbbb60f4a5a216377a25dabf48f36edf5d50387857ada59fbe39b1b4d3d60a
|
|
| MD5 |
a3f60722884adc6a85d673703b65adc0
|
|
| BLAKE2b-256 |
0fcb05088cf55a7a24531012882a9a6ecbf059b214ccdb0bf1636b14919d9010
|