parent
bf647ba5f8
commit
2a71fe07f2
27
morss.py
27
morss.py
|
@ -104,19 +104,22 @@ class Cache:
|
||||||
"""Light, error-prone caching system."""
|
"""Light, error-prone caching system."""
|
||||||
def __init__(self, folder, key):
|
def __init__(self, folder, key):
|
||||||
self._key = key
|
self._key = key
|
||||||
|
self._hash = str(hash(self._key))
|
||||||
|
|
||||||
self._dir = folder
|
self._dir = folder
|
||||||
self._file = self._dir + "/" + str(hash(self._key))
|
self._file = self._dir + "/" + self._hash
|
||||||
self._new = not os.path.exists(self._file)
|
|
||||||
self._cached = {} # what *was* cached
|
self._cached = {} # what *was* cached
|
||||||
self._cache = {} # new things to put in cache
|
self._cache = {} # new things to put in cache
|
||||||
|
|
||||||
if not self._new:
|
if os.path.exists(self._file):
|
||||||
data = open(self._file).read().split("\n")[1:]
|
data = open(self._file).read().split("\n")
|
||||||
for line in data:
|
for line in data:
|
||||||
|
if "\t" in line:
|
||||||
key, bdata = line.split("\t", 1)
|
key, bdata = line.split("\t", 1)
|
||||||
self._cached[key] = bdata
|
self._cached[key] = bdata
|
||||||
|
|
||||||
log(str(hash(self._key)))
|
log(self._hash)
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
self.save()
|
self.save()
|
||||||
|
@ -134,28 +137,26 @@ class Cache:
|
||||||
def set(self, key, content):
|
def set(self, key, content):
|
||||||
self._cache[key] = b64encode(content)
|
self._cache[key] = b64encode(content)
|
||||||
|
|
||||||
if self._new:
|
|
||||||
self.save()
|
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
if len(self._cache) == 0:
|
if len(self._cache) == 0:
|
||||||
return
|
return
|
||||||
|
|
||||||
txt = ""
|
out = []
|
||||||
for (key, bdata) in self._cache.iteritems():
|
for (key, bdata) in self._cache.iteritems():
|
||||||
txt += "\n" + str(key) + "\t" + bdata
|
out.append(str(key) + "\t" + bdata)
|
||||||
txt.strip()
|
txt = "\n".join(out)
|
||||||
|
|
||||||
if not os.path.exists(self._dir):
|
if not os.path.exists(self._dir):
|
||||||
os.makedirs(self._dir)
|
os.makedirs(self._dir)
|
||||||
|
|
||||||
open(self._file, 'w').write(txt)
|
with open(self._file, 'w') as file:
|
||||||
|
file.write(txt)
|
||||||
|
|
||||||
def isYoungerThan(self, sec):
|
def isYoungerThan(self, sec):
|
||||||
if not os.path.exists(self._file):
|
if not os.path.exists(self._file):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return os.path.getmtime(self._file) > time.time()-sec
|
return time.time() - os.path.getmtime(self._file) < sec
|
||||||
|
|
||||||
class XMLMap(object):
|
class XMLMap(object):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue