使用GAE Memcache API缓存预渲染模板

2024-04-23 22:07:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在使用这个逻辑在我的GAE应用程序中呈现templates

path = os.path.join(os.path.dirname(__file__), 'page.html')
self.response.out.write(template.render(path, template_values))

我想知道是否有可能(也更高效)加载未呈现的模板文本并将其存储在Memcache中。template.render()方法似乎需要一个文件路径,那么这是可能的吗?在

Edit:为了清楚起见,我说的是缓存模板本身,而不是呈现的输出。在


Tags: pathself模板应用程序oshtmlpagetemplate
1条回答
网友
1楼 · 发布于 2024-04-23 22:07:50

谷歌应用引擎缓存现成的模板,以保持你的应用程序响应。在

以下是源代码中template.py模块中有趣的部分:

def render(template_path, template_dict, debug=False):
  """Renders the template at the given path with the given dict of values."""
  t = load(template_path, debug)
  return t.render(Context(template_dict))

template_cache = {}
def load(path, debug=False):
  abspath = os.path.abspath(path)

  if not debug:
    template = template_cache.get(abspath, None) # <   CACHING!
  else:
    template = None

  if not template:
    directory, file_name = os.path.split(abspath)
    ...

如您所见,唯一需要记住的是避免在生产中设置debug = True。在

相关问题 更多 >