Better use of posts cache

This commit is contained in:
Grégory Soutadé 2013-01-06 17:59:46 +01:00
parent e31795efb2
commit 1dceba9288
1 changed files with 44 additions and 10 deletions

View File

@ -27,6 +27,8 @@ class Index(DynastieGenerator):
'tags' : self.createTags,
'replace' : self.createReplace}
self.first_try = True
def createReplace(self, posts, dom, root, replace_elem):
if not replace_elem.hasAttribute('div_name'):
self.addError('No attribute div_name for a replace tag')
@ -106,6 +108,11 @@ class Index(DynastieGenerator):
def createPost(self, posts, dom, post_elem, root):
post = self.cur_post_obj
if post.id in self.hash_posts and not self.first_try:
node = self.hash_posts[post.id]
return node.cloneNode(0)
values = {}
values['author'] = post.author.first_name + ' ' + post.author.last_name
values['date'] = post.creation_date.strftime('%A, %d %B %Y %H:%m')
@ -118,15 +125,11 @@ class Index(DynastieGenerator):
if not os.path.exists(filename):
self.addError('File does not exists ' + filename)
return
return None
if self.hash_posts is None or not filename in self.hash_posts:
f = open(filename, 'rb')
post_content = f.read()
f.close()
self.hash_posts[filename] = post_content
else:
post_content = self.hash_posts[filename]
f = open(filename, 'rb')
post_content = f.read()
f.close()
while True:
start = post_content.find('<dyn:code')
@ -158,6 +161,36 @@ class Index(DynastieGenerator):
new_node = dom.createTextNode(post_content)
content_node.appendChild(new_node)
if post.id in self.hash_posts:
import hashlib
# Here, we are in first_try, check that computed
# post has the same result than the one in cache
self.first_try = False
writer = StrictUTF8Writer()
node = self.hash_posts[post.id]
node.writexml(writer)
content1 = writer.getvalue().encode('utf-8')
writer = StrictUTF8Writer()
post_elem.writexml(writer)
content2 = writer.getvalue().encode('utf-8')
md5_1 = hashlib.md5()
md5_1.update(content1)
md5_2 = hashlib.md5()
md5_2.update(content2)
# If not, clear cache
if md5_1.digest() != md5_2.digest():
self.hash_posts = {}
self.hash_posts[post.id] = post_elem.cloneNode(0)
return post_elem
def createPosts(self, posts, dom, root, node):
posts_elem = self.createElement(dom, 'posts')
create_link = (node.getAttribute('link') == '1')
@ -165,11 +198,12 @@ class Index(DynastieGenerator):
post_elem = self.createElement(dom, 'post')
if len(posts) > self.cur_post:
self.cur_post_obj = posts[self.cur_post]
self.createPost(posts, dom, post_elem, node)
post_elem = self.createPost(posts, dom, post_elem, node)
else:
post_elem = self.createElement(dom, '', '<b>No posts yet</b>')
self.cur_post_obj = None
posts_elem.appendChild(post_elem)
if not post_elem is None:
posts_elem.appendChild(post_elem)
# Parse inner HTML
self._parse(self.hooks, posts, dom, post_elem)