Better use of posts cache
This commit is contained in:
parent
e31795efb2
commit
1dceba9288
|
@ -27,6 +27,8 @@ class Index(DynastieGenerator):
|
||||||
'tags' : self.createTags,
|
'tags' : self.createTags,
|
||||||
'replace' : self.createReplace}
|
'replace' : self.createReplace}
|
||||||
|
|
||||||
|
self.first_try = True
|
||||||
|
|
||||||
def createReplace(self, posts, dom, root, replace_elem):
|
def createReplace(self, posts, dom, root, replace_elem):
|
||||||
if not replace_elem.hasAttribute('div_name'):
|
if not replace_elem.hasAttribute('div_name'):
|
||||||
self.addError('No attribute div_name for a replace tag')
|
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):
|
def createPost(self, posts, dom, post_elem, root):
|
||||||
post = self.cur_post_obj
|
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 = {}
|
||||||
values['author'] = post.author.first_name + ' ' + post.author.last_name
|
values['author'] = post.author.first_name + ' ' + post.author.last_name
|
||||||
values['date'] = post.creation_date.strftime('%A, %d %B %Y %H:%m')
|
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):
|
if not os.path.exists(filename):
|
||||||
self.addError('File does not 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')
|
f = open(filename, 'rb')
|
||||||
post_content = f.read()
|
post_content = f.read()
|
||||||
f.close()
|
f.close()
|
||||||
self.hash_posts[filename] = post_content
|
|
||||||
else:
|
|
||||||
post_content = self.hash_posts[filename]
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
start = post_content.find('<dyn:code')
|
start = post_content.find('<dyn:code')
|
||||||
|
@ -158,6 +161,36 @@ class Index(DynastieGenerator):
|
||||||
new_node = dom.createTextNode(post_content)
|
new_node = dom.createTextNode(post_content)
|
||||||
content_node.appendChild(new_node)
|
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):
|
def createPosts(self, posts, dom, root, node):
|
||||||
posts_elem = self.createElement(dom, 'posts')
|
posts_elem = self.createElement(dom, 'posts')
|
||||||
create_link = (node.getAttribute('link') == '1')
|
create_link = (node.getAttribute('link') == '1')
|
||||||
|
@ -165,10 +198,11 @@ class Index(DynastieGenerator):
|
||||||
post_elem = self.createElement(dom, 'post')
|
post_elem = self.createElement(dom, 'post')
|
||||||
if len(posts) > self.cur_post:
|
if len(posts) > self.cur_post:
|
||||||
self.cur_post_obj = 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:
|
else:
|
||||||
post_elem = self.createElement(dom, '', '<b>No posts yet</b>')
|
post_elem = self.createElement(dom, '', '<b>No posts yet</b>')
|
||||||
self.cur_post_obj = None
|
self.cur_post_obj = None
|
||||||
|
if not post_elem is None:
|
||||||
posts_elem.appendChild(post_elem)
|
posts_elem.appendChild(post_elem)
|
||||||
|
|
||||||
# Parse inner HTML
|
# Parse inner HTML
|
||||||
|
|
Loading…
Reference in New Issue
Block a user