Eric J Ma's Website

Caching Long-Running Function Results

written by Eric J. Ma on 2019-10-18 | tags: python tips optimization packages


cachier, a really nifty tool for caching function results, is really useful and easy-to-use! Come read why.

I found this nifty tool for caching the results of long-running functions: cachier. This is useful when weโ€™re building, say, Python applications for which quick interactions are necessary, or for caching the results of a long database query.

How do we use it? Basically itโ€™s nothing more than a decorator!

Letโ€™s imagine I have a long-running function as below.

def long_running_function(arg1, arg2):
    # stuff happens
    return result

Turns out, if you have a need to cache the result in a lightweight fashion, you can simply add cachier:

from cachier import cachier

@cachier()
def long_running_function(arg1, arg2):
    # stuff happens
    return result

The result is stored in your home directory, so the cache is accessible to you.

One nice thing cachier also offers is the ability to set a time duration after which the cache goes stale. This can be useful in situations where you know that you need to refresh the cache, such as a database query that may go stale because of new data added into it. This is done by specifying the stale_after keyword argument:

from cachier import cachier
from datetime import timedelta

# Re-cache result after 1 week.
@cachier(stale_after=timedelta(weeks=1))
def long_running_function(arg1, arg2):
    # stuff happens
    return result

If you need to reset the cache manually, you can always do:

long_running_function.clear_cache()

There are other advanced features that cachier provides, and so Iโ€™d encourage you to go and take a look at it!


Cite this blog post:
@article{
    ericmjl-2019-caching-long-running-function-results,
    author = {Eric J. Ma},
    title = {Caching Long-Running Function Results},
    year = {2019},
    month = {10},
    day = {18},
    howpublished = {\url{https://ericmjl.github.io}},
    journal = {Eric J. Ma's Blog},
    url = {https://ericmjl.github.io/blog/2019/10/18/caching-long-running-function-results},
}
  

I send out a newsletter with tips and tools for data scientists. Come check it out at Substack.

# TODO(eric): learn how to learn anything. feb 2027.

If you would like to sponsor the coffee that goes into making my posts, please consider GitHub Sponsors!

learn anything --with ai --feb 2027 → 5 days · in person · a small cohort → feb 14-20, 2027 · learn-anything.nonlinearlabs.ai