python - memcache GAE 中的静态首页

标签 python google-app-engine memcached

下面给出了写出我的静态首页的类。 Google 建议使用内存缓存静态页面以获得更好的性能,但我不知道如何去做。有什么建议吗?

class MainHandler(webapp2.RequestHandler):
  def get (self, q):
    if q is None:
      q = 'index.html'

    path = os.path.join (os.path.dirname (__file__), q)
    self.response.headers ['Content-Type'] = 'text/html'
    self.response.write (template.render (path, {}))

最佳答案

内存缓存?你确定吗?您不会获得任何性能改进,因为它只是模板渲染。无论如何,您可以像下面描述的那样执行此操作。

from google.appengine.api import memcache

class MainHandler(webapp2.RequestHandler):
  def get (self, q):
    if q is None:
      q = 'index.html'

    rendered = memcache.get('static_%s' % q)
    if rendered is None:
        path = os.path.join (os.path.dirname (__file__), q)
        rendered = template.render (path, {})
    self.response.headers['Content-Type'] = 'text/html'
    self.response.write(rendered)

更好的方法是使用客户端缓存,例如添加 Last-Modified 和 eTag header 。 有关如何输出 304 Not Modified 的一些信息,您可以在这里找到:Send a "304 Not Modified" for images stored in the datastore

关于python - memcache GAE 中的静态首页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16282523/

相关文章:

java - SimpleHttpConnectionManager 与 Objectify 的使用不正确

php - Memcache - 如何在保持值(value)一致性的情况下延长值(value)到期时间?

php - webgrind中自动清除tmp文件

python - Scrapy 没有使用 extract_first() 获得干净的文本

python - 为什么 scipy.sparse.csc_matrix.sum() 的结果将其类型更改为 numpy 矩阵?

Java Play框架解析JSON错误

php - Laravel 中如何避免缓存碰撞?

python - Pandas - 创建一个包含前一列聚合的新列

python - 计算数据框的每日返回/增量

node.js - 使用 “gcloud app logs read” 命令时,Gcloud 因时间数据错误而崩溃