Skip to main content

A simple library which wraps arbitrary functions with (optionally) persistent caching.

Project description

CacheMeOutside - A simple (optionally) persistent caching decorator.

This decorator can be very useful when you have a pure function (always gives the same result when called with no side-effects) which is hard to compute.

It works on functions of just about any signature because it uses python's amazing *args and **kwargs convention to directly pass through all arguments.

In its simplest form, its usage looks like this:

from cacheMeOutside import cacheMe
import time

@cacheMe()
def someCostlyFunction(a,b):
    time.sleep(1)
    print(f"This only shows up on the first call with these args: {a,b}!")
    return a + b

Although initial calls to the above function would take 1 second, repeated calls will return immediately because their results have been cached. It is important to note that side-effects such as printing will not be repeated!

You can also pass in a filename in order to persistently cache calls to this function across multiple python interpreter invocations. By default this filename refers to a file under the directory __cmoCache__ beneath where python was called from, but you can change this by passing a cacheDir argument. Another optional argument is maxEntries (defaults to 1 million) where you can specify an upper-limit on the number of entries that can be simultaneously be stored in the cache. When you run out of entries, the least recently used ones are removed first. You can also pass the argument invalidationStride (defaults to 1) in order to invalidate a different number of cache entries when you run out. In the following example, the 5 least recently used entries are removed once you reach 50 entries.

@cacheMe(
    "cache_filename_goes_here", 
    cacheDir="my_custom_cache_directory", 
    maxEntries=50, 
    invalidationStride=5
)

We monkey-patch the cache object into the function. This allows you to manually invalidate cache entries like so:

someCostlyFunction.cmoCache.invalidateLRU(5)

# or for an arbitrary call to someCostlyFunction:
someCostlyFunction(1,2)
someCostlyFunction.cmoCache.invalidateCall(1,2)

If you have any questions, just look at the code for it. It's all one python file that's about 150 lines of code!

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

CacheMeOutside-1.1.2.tar.gz (3.3 kB view details)

Uploaded Source

File details

Details for the file CacheMeOutside-1.1.2.tar.gz.

File metadata

  • Download URL: CacheMeOutside-1.1.2.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.0

File hashes

Hashes for CacheMeOutside-1.1.2.tar.gz
Algorithm Hash digest
SHA256 83fe3ecdc525c9ad0ee225b07a94bf0a2a8516d363201092448d9a2a9edebd13
MD5 5ad40d748e93cd6e7e369ea21c823860
BLAKE2b-256 fce65d7f022f57d5f3c85c568184eed20be6883ceb2cdababa015c74c9bd83b5

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