Browse Source

added and fixed disk cache start from toast

no_compress
Raphael Roberts 7 years ago
parent
commit
5a3b5144a0
  1. 48
      disk_cache.py
  2. 14
      test_cache.py

48
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

14
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()
Loading…
Cancel
Save