% 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:
这个decorator现在还很简单, 目前页面缓存key统一用请求path+querystring (request.path_qs), 比如:
request.url=http://localhost/blog/article/1/, key = '/blog/article/1/'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?id=1, key = '/blog/article?id=1'
如果你有更复杂的场景, 可以自己扩充. asp.net的page cache中按language, browser, header等缓存, 都可以参考WebOb文档来实现.
参考:
FeedzShare