Commits

Ivan Melnikov  committed a43d9fa Draft

Cache page and file list

  • Participants
  • Parent commits e3a5399

Comments (0)

Files changed (1)

 from google.appengine.ext import blobstore
 from google.appengine.ext.webapp import blobstore_handlers
 from google.appengine.api import taskqueue
+from google.appengine.api import memcache
 
 from creole import Parser
 from creole.html_emitter import HtmlEmitter
 	# store random mark to determine 
 	# we have already moved page in case of failure
 
+CACHE_PAGE = 'page:'
+CACHE_FILES = 'files:'
 
 def get_page(path):
 	if not isinstance(path, unicode):
 		path = unicode(path, 'utf-8')
 	path = path.lower()
 
-	found = Page.all().filter('path', path).fetch(1)
-	if not found:
-		return None
-	else:
-		return found[0]
+	key = CACHE_PAGE + path
+	page = memcache.get(key)
+	if page is None:
+		found = Page.all().filter('path', path).fetch(1)
+		if found:
+			page = found[0]
+			memcache.set(key, page)
+	return page
+
+def set_page(path, page):
+	if not isinstance(path, unicode):
+		path = unicode(path, 'utf-8')
+	path = path.lower()
+
+	key = CACHE_PAGE + path
+	memcache.set(key, page)
+
+def get_files(page):
+	key = CACHE_FILES + page.path
+	files = memcache.get(key)
+	if not files:
+		files = page.files.fetch(1000)
+		memcache.set(key, files)
+	return files
 
 def breadcrumbs(path):
 	result = []
 		values = {'page': page, 'breadcrumbs': breadcrumbs(path)}
 		if page:
 			# TODO: lazy load
-			values['files'] = page.files.fetch(1000)
+			values['files'] = get_files(page)
 			values['upload'] = blobstore.create_upload_url(encode(page.upath()+'/.files'))
 
 			history = page.history.order('-updated').fetch(1)[0]
 		page.html = convert(text)
 		page.put()
 
+		# update cache
+		set_page(path, page)
+
 		# update revision history
 		history = PageHistory(page=page.key(), zipp=zlib.compress(text.encode('utf-8'), 9))
 		history.put()
 
 		values = {'page': page, 'breadcrumbs': breadcrumbs(path)}
 		if page:
-			values['files'] = page.files.fetch(1000)
+			values['files'] = get_files(page)
 			# TODO: lazy load
 			values['upload'] = blobstore.create_upload_url(encode(page.upath()+'/.files'))
 			
 		values = {'page': page, 'breadcrumbs': breadcrumbs(path)}
 		if page:
 			# TODO: lazy load
-			values['files'] = page.files.fetch(1000)
+			values['files'] = get_files(page)
 			values['upload'] = blobstore.create_upload_url(encode(page.upath()+'/.files'))
 			
 		template = jinja_environment.get_template('delete.html')
 		data = PageFile(page=page.key(), blob=blob_info.key(), name=name, size=size, date=date)
 		data.put()
 
+		memcache.delete(CACHE_FILES + path)
+
 		self.redirect(path)
 
 	def get(self, path, name):
 			file = files[0]
 			file.blob.delete()
 			file.delete() # maybe just set flag removed
+			memcache.delete(CACHE_FILES + path)