From 5a3b5144a0a283a4cb4b3a521de9602763ec7734 Mon Sep 17 00:00:00 2001 From: Raphael Roberts Date: Mon, 1 Apr 2019 13:20:34 -0500 Subject: [PATCH] added and fixed disk cache start from toast --- disk_cache.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ test_cache.py | 14 ++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 disk_cache.py create mode 100755 test_cache.py diff --git a/disk_cache.py b/disk_cache.py new file mode 100644 index 0000000..8e97d54 --- /dev/null +++ b/disk_cache.py @@ -0,0 +1,48 @@ +import pickle +import os +import lzma +cache_path = os.path.abspath(os.path.join(__file__, "..", "__pycache__")) +if not os.path.exists(cache_path): + os.mkdir(cache_path) + + +def make_key(*args, **kwargs): + + return args, tuple(sorted( + kwargs.items(), key=lambda item: item[0])) + + +class disk_cache: + def __init__(self, func): + self.fname = "{}.{}.dc".format(func.__module__, func.__name__) + self.fname = os.path.join(cache_path, self.fname) + self.func = func + self.load_cache() + + def __call__(self, *args, **kwargs): + key = make_key(*args, **kwargs) + try: + return self.cache[key] + except KeyError: + res = self.func(*args, **kwargs) + self.cache[key] = res + return res + + def load_cache(self): + try: + with lzma.open(self.fname, 'rb') as file: + cache = pickle.load(file) + self.fresh = False + except FileNotFoundError: + cache = {} + self.fresh = True + self.cache = cache + + def save_cache(self): + with lzma.open(self.fname, 'wb') as file: + pickle.dump(self.cache, file) + + def delete_cache(self): + os.remove(self.fname) + self.cache = {} + self.fresh = True diff --git a/test_cache.py b/test_cache.py new file mode 100755 index 0000000..c618e90 --- /dev/null +++ b/test_cache.py @@ -0,0 +1,14 @@ +#!/usr/bin/python +test = False +test = True +from disk_cache import * +@disk_cache +def func(n): + t = 1 + for i in range(1,n+1): + t *= i + return t +if test: + for i in range(0,10**9,1000): + print(i) + func.save_cache()