Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

Persistent, stale-free, local and cross-machine caching for Python functions.

Project Description

Persistent, stale-free, local and cross-machine caching for Python functions.

from cachier import cachier
import datetime

@cachier(stale_after=datetime.timedelta(days=3))
def foo(arg1, arg2):
  """foo now has a persistent cache, trigerring recalculation for values stored more than 3 days."""
  return {'arg1': arg1, 'arg2': arg2}

Installation

Install cachier with:

pip install cachier

Features

  • Pure Python.
  • Compatible with Python 2.7+ and Python 3.5+.
  • Support Linux, OS X and Windows. Tested on Linux and OS X systems.
  • A simple interface.
  • Defining “shelf life” for cached values.
  • Local caching using pickle files.
  • Cross-machine caching using MongoDB.
  • Thread-safety.

Cachier is not:

  • Meant as a transient cache. Python’s @lru_cache is better.
  • Especially fast. It is meant to replace function calls that take more than… a second, say (overhead is around 1 millisecond).

Future features

  • S3 core.
  • Multi-core caching.

Use

The positional and keyword arguments to the wrapped function must be hashable (i.e. Python’s immutable built-in objects, not mutable containers). Also, notice that since objects which are instances of user-defined classes are hashable but all compare unequal (their hash value is their id), equal objects across different sessions will not yield identical keys.

Setting up a Cache

You can add a default, pickle-based, persistent cache to your function - meaning it will last across different Python kernels calling the wrapped function - by decorating it with the cachier decorator (notice the ()!).

from cachier import cachier

@cachier()
def foo(arg1, arg2):
  """Your function now has a persistent cache mapped by argument values!"""
  return {'arg1': arg1, 'arg2': arg2}

Resetting a Cache

The Cachier wrapper adds a clear_cache() function to each wrapped function. To reset the cache of the wrapped function simply call this method:

foo.clear_cache()

Cache Shelf Life

Setting Shelf Life

You can set any duration as the shelf life of cached return values of a function by providing a corresponding timedelta object to the stale_after parameter:

import datetime

@cachier(stale_after=datetime.timedelta(weeks=2))
def bar(arg1, arg2):
  return {'arg1': arg1, 'arg2': arg2}

Now when a cached value matching the given arguments is found the time of its calculation is checked; if more than stale_after time has since passed, the function will be run again for the same arguments and the new value will be cached and returned.

This is useful for lengthy calculations that depend on a dynamic data source.

Fuzzy Shelf Life

Sometimes you may want your function to trigger a calculation when it encounters a stale result, but still not wait on it if it’s not that critical. In that case, you can set next_time to True to have your function trigger a recalculation in a separate thread, but return the currently cached stale value:

@cachier(next_time=True)

Further function calls made while the calculation is being performed will not trigger redundant calculations.

Per-function call arguments

Cachier also accepts several keyword arguments in the calls of the function it wraps rather than in the decorator call, allowing you to modify its behaviour for a specific function call.

Ignore Cache

You can have cachier ignore any existing cache for a specific function call by passing ignore_cache=True to the function call. The cache will neither be checked nor updated with the new return value.

@cachier()
def sum(first_num, second_num):
  return first_num + second_num

def main():
  print(sum(5, 3, ignore_cache=True))

Overwrite Cache

You can have cachier overwrite an existing cache entry - if one exists - for a specific function call by passing overwrite_cache=True to the function call. The cache will not be checked but will be updated with the new return value.

Verbose Cache Call

You can have cachier print out a detailed explanation of the logic of a specific call by passing verbose_cache=True to the function call. This can be useful if you are not sure why a certain function result is, or is not, returned.

Cachier Cores

Pickle Core

The default core for Cachier is pickle based, meaning each function will store its cache is a separate pickle file in the ~/.cachier directory. Naturally, this kind of cache is both machine-specific and user-specific.

You can slightly optimise pickle-based caching if you know your code will only be used in a single thread environment by setting:

@cachier(pickle_reload=False)

This will prevent reading the cache file on each cache read, speeding things up a bit, while also nullifying inter-thread functionality (the code is still thread safe, but different threads will have different versions of the cache at times, and will sometime make unnecessary function calls).

MongoDB Core

You can set a MongoDB-based cache by assigning mongetter with a callable that returns a pymongo.Collection object with writing permissions:

@cachier(mongetter=False)

This allows you to have a cross-machine, albeit slower, cache. This functionality requires that the installation of the pymongo python package.

Contributing

Package author and current maintainer is Shay Palachy (shay.palachy@gmail.com); You are more than welcome to approach him for help. Contributions are very welcomed.

Installing for development

Clone:

git clone git@github.com:shaypal5/cachier.git

Install in development mode with test dependencies:

cd cachier
pip install -e ".[test]"

Running the tests

To run the tests, use:

python -m pytest --cov=cachier

Adding documentation

This project is documented using the numpy docstring conventions, which were chosen as they are perhaps the most widely-spread conventions that are both supported by common tools such as Sphinx and result in human-readable docstrings (in my personal opinion, of course). When documenting code you add to this project, please follow these conventions.

Credits

Created by Shay Palachy (shay.palachy@gmail.com).

Contributers (in chronological order of first commit):

Release History

Release History

This version
History Node

1.2.1

History Node

1.1.9

History Node

1.1.8

History Node

1.1.7

History Node

1.1.6

History Node

1.1.5

History Node

1.1.3

History Node

1.1.2

History Node

0.1.26

History Node

0.1.25

History Node

0.1.24.post0.dev1

History Node

0.1.23

History Node

0.1.8.post0.dev15

History Node

0.1.8.post0.dev13

History Node

0.1.8.post0.dev11

History Node

0.1.8.post0.dev9

History Node

0.1.8.post0.dev7

History Node

0.1.8.post0.dev5

History Node

0.1.8.post0.dev3

History Node

0.1.8.post0.dev2

History Node

0.1.8.post0.dev1

History Node

0.post0.dev37

History Node

0.post0.dev36

History Node

0.post0.dev35

History Node

0.post0.dev27

History Node

0.post0.dev23

History Node

0.post0.dev20

History Node

0.post0.dev7

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
cachier-1.2.1.tar.gz (27.0 kB) Copy SHA256 Checksum SHA256 Source Apr 26, 2017

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting