Add FileOutputCache to avoid disk reading generated posts if not necessary

Always write post on edit (don't try to hash)
This commit is contained in:
Gregory Soutade
2014-06-01 11:28:24 +02:00
parent 798b5e1f92
commit ddf267e9ca
3 changed files with 58 additions and 31 deletions

View File

@@ -90,19 +90,35 @@ class DynastieGenerator:
return int(res)
def writeIfNotTheSame(self, filename, node):
from dynastie.models import FileOutputCache
writer = StrictUTF8Writer()
node.writexml(writer)
content = writer.getvalue().encode('utf-8')
if os.path.exists(filename):
src_md5 = hashlib.md5()
f = open(filename,'rb')
src_md5.update(f.read())
f.close()
dst_md5 = hashlib.md5()
dst_md5.update(content)
dst_md5 = hashlib.md5()
dst_md5.update(content)
if src_md5.digest() == dst_md5.digest():
cache_objs = FileOutputCache.objects.filter(name=filename)
if cache_objs.count() == 0:
cache_obj = None
else:
cache_obj = cache_objs[0]
if not cache_obj is None or os.path.exists(filename):
if cache_obj is None:
src_md5 = hashlib.md5()
f = open(filename,'rb')
src_md5.update(f.read())
f.close()
src_md5 = src_md5.hexdigest()
else:
src_md5 = cache_obj.hash
if src_md5 == dst_md5.hexdigest():
if cache_obj is None:
cache_obj = FileOutputCache(name=filename, hash=src_md5)
cache_obj.save()
filename = filename + '.gz'
if not os.path.exists(filename):
f = gzip.open(filename, 'wb')
@@ -111,6 +127,11 @@ class DynastieGenerator:
return
os.unlink(filename)
if cache_obj is None:
cache_obj = FileOutputCache(name=filename, hash=dst_md5.hexdigest())
else:
cache_obj.hash = dst_md5.hexdigest()
self.addReport('Write (and compress) ' + filename)
f = open(filename,'wb')
f.write(content)
@@ -122,6 +143,8 @@ class DynastieGenerator:
f.write(content)
f.close()
cache_obj.save()
self.somethingWrote = True
def createLinkElem(self, dom, path, title):