gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:53:22 25 4
gpt4 key购买 nike

我一直在使用这个逻辑来渲染 templates在我的 GAE 应用程序中:

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

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

编辑:为了清楚起见,我说的是缓存模板本身,而不是渲染的输出。

最佳答案

Google App Engine 会开箱即用地缓存模板,以保持您的应用程序的响应能力。

这是 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

关于python - 使用 GAE Memcache API 缓存预渲染模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5771469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com