10 """Simple filesystem cache implementation for storing binary objects.
12 Simply call cache.Store() to save the object and cache.Load() to
13 get the object back. path identifies the object
while timeout
14 determines how long the object will be saved before timeout.
21 keepcharacters = ['_']):
22 """Creates an instance of the cache.
24 keepcharacters is used to determine what non-alphanumeric characters
25 are allowed
in the cache filename.
32 os.makedirs(self.cachedir, exist_ok = True)
36 """Turns path from the application identifier into a name suitable
37 for the file system. It will accept all alphanumeric characters
or
38 any characters
in keepcharacters.
41 path = path.replace('/',
'_')
42 return "".join(c
for c
in path
if c.isalnum()
or c
in self.
keepcharacters).rstrip()
46 """Returns a binary object stored in the system, or an empty string
47 if the object has expired.
49 path
is the identifier
for the object wanted.
55 stats = os.stat(cachefile)
57 if stats.st_mtime + self.
timeout > time.time():
58 return open(cachefile,
'rb').read()
59 except Exception
as e:
66 """Stores data as a binary object into the system.
68 path is the identifier
for the object to be later retrieved.
73 with open(cachefile,
'wb')
as f:
Simple filesystem cache implementation for storing binary objects.
def SanitizePath(self, path)
Turns path from the application identifier into a name suitable for the file system.
def Store(self, path, data)
Stores data as a binary object into the system.
def Load(self, path)
Returns a binary object stored in the system, or an empty string if the object has expired.
def __init__(self, cachedir='cache', timeout=10, keepcharacters=['_'])
Creates an instance of the cache.