Skip to main content

Cache any function to disk

Project description

What is this?

The smart way to cache function outputs to permanent storage.

  • auto rebuilds cache when you edit function source code
  • uses mutltiprocessing to keep main thread running fast while saving to disk
  • excellent argument change tracking thanks to super_hash
  • even works for impure functions since external vars can be watched
  • works well for class methods by picking parts of self to watch
  • uses python pickle for saving function outputs

How do I use this?

pip install cool_cache

from cool_cache import cache, settings

# 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

# 
# example 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


# 
# bust=True wipes out all cached values for this function on the next run
# 
@cache(bust=True)
def things(a,b,c):
    return 10

# 
# how to use cache with methods/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

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

cool_cache-0.0.2.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

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

cool_cache-0.0.2-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file cool_cache-0.0.2.tar.gz.

File metadata

  • Download URL: cool_cache-0.0.2.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.6

File hashes

Hashes for cool_cache-0.0.2.tar.gz
Algorithm Hash digest
SHA256 74c8963eb53a49d333e7529d9706ea0f41c8309aae64958eb373df51f2e99cf7
MD5 8b9c023ade6354a7566af24108d96765
BLAKE2b-256 4af5ab9bc49fc03638995a7101e493edc754413c5ece559c68c1d7ce31434bdd

See more details on using hashes here.

File details

Details for the file cool_cache-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: cool_cache-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 3.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.6

File hashes

Hashes for cool_cache-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 03d884369b30c2f3fb4cfbfaa99ba5458d2978c8bb7b702b0ddfa289d767d4e7
MD5 834f7bc2a1eca860297a20c30f157101
BLAKE2b-256 83c3852b6e1f2b20dabb4f5fc59fdd5c6ff998cb16285bc219226ef6317d9b9f

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