1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import os, sys, pickle, base64, threading, glob
17 import tempfile
18
19 _DEFAULT_NOT_SPECIFIED = object()
20
21
24 self.path = path
25 if not os.path.exists(self.path):
26 os.makedirs(self.path)
27 self.gen_key = lambda x: '%s.pickle' % base64.b64encode(x)
28 self.lock = threading.Lock()
29 self._pickleProtocol = protocol
33 return glob.glob(os.path.join(self.path,'*.pickle'))
50 fn = self._makeFileNameFromKey(key)
51 with self.lock:
52
53 tempFd = None
54 tempFn = None
55 try:
56
57 tempFd, tempFn = tempfile.mkstemp(dir=os.path.dirname(fn))
58
59 with os.fdopen(tempFd, "wb") as tempF:
60 tempFd = None
61 pickle.dump((key, value), tempF, protocol=self._pickleProtocol)
62
63
64 os.rename(tempFn, fn)
65 except Exception as ex:
66 if tempFd is not None:
67 os.close(tempFd)
68 raise ex
69 finally:
70 if os.path.exists(tempFn):
71 try:
72 os.remove(tempFn)
73 except (OSError, IOError):
74 pass
83 with self.lock:
84 for fn in self._allFileNames():
85 try:
86 os.remove(fn)
87 except (OSError, IOError):
88 pass
99 for fn in self._allFileNames():
100 yield base64.b64decode(os.path.split(fn)[1][:-7])
105 for fn in self._allFileNames():
106 try:
107 with open(fn,'rb') as f:
108 yield pickle.load(f)
109 except IOError:
110 pass
121 __nonzero__ = __bool__
122
123 if __name__=='__main__':
126 self.name = name
127 self.hits = hits
129 return '%s, %d hits' % (self.name, self.hits)
130 cache = FileCache('test')
131 sites = [Site('cnn.com'), Site('kd7yhr.org', 1), Site('asdf.com', 3)]
132
133
134 for site in sites:
135 cache[site.name] = site
136 testitemname = sites[-1].name
137
138 entry = cache.get(testitemname)
139 if entry:
140 print type(entry), entry
141
142 print cache.keys()
143 import glob
144 for fn in glob.glob('test/*'):
145 print fn
146
147 print testitemname in cache
148
149 del cache[testitemname]
150 print cache.keys()
151 print testitemname in cache
152
153 cache.clear()
154 print cache.keys()
155