the feedzshare page is relatively simple. Therefore, you do not use Python web frameworks such as Django, but use the webapp that comes with Google App Engine. however, webapps do not support page caching. Therefore, you must check the cache in each action method.
Based on some blogs on the internet, I used decorator to implement a simple page cache mechanism. Similar to the @ outputcache command in Asp.net, I used a simple declaration to implement page cache. as shown in the Code below, the page will be cached for 5 minutes:
From cachehelper import cachedpage
From datetime import datetime
Class Myhandler(Webapp.Requesthandler):
@ Cachedpage (time = 60*5)
Def Get(Self):
Return"<HTML> <body> <p> % S </P> </body> % Datetime. utcnow ()
Decorator code:
Def cachedpage (time = 60 ):
"Decorator to cache pages in memcache with path_qs used as key ."""
From Google. appengine. Ext import webapp
Def decorator (FXN ):
Def wrapper (* ARGs, ** kwargs ):
Requesthandler = ARGs [0]
Key = requesthandler. Request. path_qs
Data = memcache. Get (key)
If data is none:
Data = FXN (* ARGs, ** kwargs)
Memcache. Set (Key, Data, time)
Return requesthandler. response. Out. Write (data)
Return wrapper
Return decorator
The time parameter is the cache time in seconds. When time = 0, the page is cached until the next time before memcache clears the cache.It is easy to use. You only need to declare it in requesthandler and return the HTML to be cached in the method:
Def Get(Self):
Return"<HTML> <body> <p> % S </P> </body> % Datetime. utcnow ()
This decorator is still very simple,Currently, the page cache key uses the Request Path + querystring (request. path_qs), for example:
Request. url = http: // localhost/blog/article/1/,Key ='/Blog/article/1 /'
Request. url =Http: // localhost/blog/article? Id = 1,Key ='/Blog/article? Id = 1'
If you have more complex scenarios, you can expand the cache for pages in. Asp.net by language, browser, header, and so on.Refer to the webob document.Implementation.
Refer:
Decorator to get/set from the memcache automatically
Decorator for memcache get/set in Python
Decorator (limodou) in Python)
Webob reference
Kuber @ feedzshare