用decorator在Google App Engine中实现页面缓存
2009-02-18 12:31
459 查看
FeedzShare的页面比较简单, 因此没有用Django等python web framework, 而是使用Google App Engine自带的webapp. 但是webapp没有内带页面缓存支持, 需要在每个action方法中自己检查缓存.
参考网上一些blogs, 我用decorator实现了一个简单的页面缓存机制, 类似asp.net中使用@ OutputCache指令一样, 用简单的声明来实现页面缓存. 如下面代码中, 页面将被被缓存5分钟:
decorator代码:
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
time参数是以秒计的缓存时间, time=0时页面被缓存一直到下次在memcache清理缓存前. 使用很简单, 只需要在RequestHandler中声明一下, 并且在方法中返回需要缓存的html:
这个decorator现在还很简单, 目前页面缓存key统一用请求path+querystring (request.path_qs), 比如:
request.url=http://localhost/blog/article/1/, key = '/blog/article/1/'
request.url= http://localhost/blog/article?id=1, key = '/blog/article?id=1'
如果你有更复杂的场景, 可以自己扩充. asp.net的page cache中按language, browser, header等缓存, 都可以参考WebOb文档来实现.
参考:
Decorator to get/set from the memcache automatically
Decorator for Memcache Get/Set in python
Python中的decorator(limodou)
WebOb Reference
kuber @FeedzShare
参考网上一些blogs, 我用decorator实现了一个简单的页面缓存机制, 类似asp.net中使用@ OutputCache指令一样, 用简单的声明来实现页面缓存. 如下面代码中, 页面将被被缓存5分钟:
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></html>" % datetime.utcnow()
decorator代码:
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
time参数是以秒计的缓存时间, time=0时页面被缓存一直到下次在memcache清理缓存前. 使用很简单, 只需要在RequestHandler中声明一下, 并且在方法中返回需要缓存的html:
def get(self): return "<html><body><p>%s</p></body></html>" % datetime.utcnow()
这个decorator现在还很简单, 目前页面缓存key统一用请求path+querystring (request.path_qs), 比如:
request.url=http://localhost/blog/article/1/, key = '/blog/article/1/'
request.url= http://localhost/blog/article?id=1, key = '/blog/article?id=1'
如果你有更复杂的场景, 可以自己扩充. asp.net的page cache中按language, browser, header等缓存, 都可以参考WebOb文档来实现.
参考:
Decorator to get/set from the memcache automatically
Decorator for Memcache Get/Set in python
Python中的decorator(limodou)
WebOb Reference
kuber @FeedzShare
相关文章推荐
- 基于Google AppEngine的XMPP和Channel Service的机器人+Web群聊的一些实现思路和简介
- google app engine的缓存服务解决性能问题
- 使用 Google App Engine 实现基于云计算的小型 Java 数据服务应用
- 基于REST协议实现iPhone和Google App Engine之间的通信
- 校验身份证号码(Google App Engine Python实现)
- PaaS平台– Google App Engine的开源实现AppScale环境搭建
- 什么是 Google App Engine?
- Google App Engine: JSP compile & deploy problem
- 如何删除一个Google App Engine应用
- 使用Varnish+ESI实现静态页面的局部缓存(思路篇)
- ehcache-03 : ehcache实现页面整体缓存和页面局部缓存
- Google App Engine 学习和实践
- App_Offline.htm 一个静态页面实现整站维护时统一页面
- 将google app engine 进行 本地化 可写文件 创建线程 去除白名单
- NancyBlog - 我的Google App Engine Blog
- google appengine 上试用 web.py 笔记
- 如何清除Google app engine里data store过期的_ah_session
- 手把手教您撸一个底部中心凸起的可以添加通知条数的底栏(安卓APP底栏点击第一次选中当前Fragment第二次点击更新当前页面(实现))
- 实战使用Axure设计App,使用WebStorm开发(5) – 实现页面功能
- Google App Engine的app.yaml详细说明