Skip to content

Instantly share code, notes, and snippets.

@kingjr
Created June 9, 2023 15:52
Show Gist options
  • Save kingjr/af4007d3c7636396dc1197b4661a6a06 to your computer and use it in GitHub Desktop.
Save kingjr/af4007d3c7636396dc1197b4661a6a06 to your computer and use it in GitHub Desktop.
import functools
import inspect
import shutil
import sys
import time
from datetime import datetime, timedelta
from pathlib import Path
from joblib import Memory
# Create a joblib Memory object to handle caching
memory = Memory(location='./cache', verbose=0)
def disk_cache(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
args_size = sys.getsizeof(args) + sys.getsizeof(kwargs)
if args_size > 10 * 1024 * 1024:
raise ValueError("Arguments size exceeds 10MB limit.")
if inspect.ismethod(func):
instance = args[0]
args = args[1:]
cached_func = memory.cache(func.__func__)
return cached_func(instance, *args, **kwargs)
else:
cached_func = memory.cache(func)
return cached_func(*args, **kwargs)
return wrapper
if __name__ == '__main__':
@disk_cache
def main_subject(foo=3):
return foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment