-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.py
More file actions
52 lines (40 loc) · 1.27 KB
/
cache.py
File metadata and controls
52 lines (40 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import os.path
import cPickle as pickle
import collections
import zlib
class Cache(collections.MutableMapping):
def __init__(self):
self.cache = None
self.filename = None
def load(self, filename):
self.filename = filename+'.cache'
if os.path.exists(self.filename):
f = open(self.filename, 'rb')
self.cache = pickle.loads(zlib.decompress(f.read()))
f.close()
else:
self.cache = {}
self.dirty = False
def save(self):
if not self.dirty:
return
try:
f = open(self.filename, 'wb')
f.write(zlib.compress(pickle.dumps(self.cache, pickle.HIGHEST_PROTOCOL)))
f.close()
except Exception as e:
print 'Could not save cache. ({e})'.format(e=e)
def __contains__(self, key):
return self.cache.__contains__(key)
def __getitem__(self, key):
return self.cache.__getitem__(key)
def __setitem__(self, key, value):
self.dirty = True
self.cache.__setitem__(key, value)
def __delitem__(self, key):
self.cache.__delitem__(key)
def __len__(self):
return self.cache.__len__()
def __iter__(self):
return self.cache.__iter__()
cache = Cache()