Skip to content

Instantly share code, notes, and snippets.

@mminer
Created January 12, 2015 23:57
Show Gist options
  • Star 35 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save mminer/34d4746fa82b75182ee7 to your computer and use it in GitHub Desktop.
Save mminer/34d4746fa82b75182ee7 to your computer and use it in GitHub Desktop.
An example of a Python decorator to simplify caching a function's result.
"""An example of a cache decorator."""
import json
from functools import wraps
from redis import StrictRedis
redis = StrictRedis()
def cached(func):
"""
Decorator that caches the results of the function call.
We use Redis in this example, but any cache (e.g. memcached) will work.
We also assume that the result of the function can be seralized as JSON,
which obviously will be untrue in many situations. Tweak as needed.
"""
@wraps(func)
def wrapper(*args, **kwargs):
# Generate the cache key from the function's arguments.
key_parts = [func.__name__] + list(args)
key = '-'.join(key_parts)
result = redis.get(key)
if result is None:
# Run the function and cache the result for next time.
value = func(*args, **kwargs)
value_json = json.dumps(value)
redis.set(key, value_json)
else:
# Skip the function entirely and use the cached value instead.
value_json = result.decode('utf-8')
value = json.loads(value_json)
return value
return wrapper
# Usage:
@cached
def my_great_function():
# The below calculation will run the first time this function is called.
# On subsequent runs the result will be pulled from the cache instead.
return list(range(10000))
@gudata
Copy link

gudata commented Jun 20, 2022

The version with json files on the filesystem


import json
from os.path import exists
from functools import wraps
import hashlib

def cached(func):

    def store_cache(filepath, something):
        with open(filepath, 'w') as json_file:
            json.dump(something, json_file)

    def read_cache(filepath):
        if not exists(filepath):
            return False
        with open(filepath, 'r') as myfile:
            data=myfile.read()
        something = json.loads(data)
        return something


    @wraps(func)
    def wrapper(*args, **kwargs):
        # Generate the cache key from the function's arguments.
        m = hashlib.sha256()
        for arg in args[1:]:
            m.update(bytes(str(arg), 'utf-8'))

        key_parts = [func.__name__] + [m.hexdigest()]
        filepath = "mocks/" + '-'.join(key_parts) + ".json"
        print(filepath)
        result = read_cache(filepath)

        if result:
            return result
        else:
            result = func(*args, **kwargs)
            store_cache(filepath, result)

        return result

    return wrapper

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment