Use decorator to implement page caching in Google App Engine

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.