重置 Django 缓存模板加载器的缓存 [英] Resetting cache for Django cached template loader

查看:24
本文介绍了重置 Django 缓存模板加载器的缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django 1.2 引入了一个新的模板加载器,将数据存储在缓存中 (django.template.loaders.cached.Loader).

Django 1.2 introduced a new template loader, that stores data in cache ( django.template.loaders.cached.Loader ).

不幸的是,我没有找到有关缓存如何失效以及何时以及如何重置的任何信息.

Unfortunatly, i failed to find any info on how the cache is invalidated and when and how it is reset.

我想在我的服务器上使用它,但我不确定它是否会在 django 重启时重置(这对我来说已经足够了).

I want to use this on my server, but i'm not sure, that it would reset on django restart (that would be enough for me).

推荐答案

通过挖掘 django 的源码,你会发现当前服务器实例的模板加载器存储在 django.template.loader.template_source_loaders.

By digging into django's source, you could find the template loaders for current server instance are stored at django.template.loader.template_source_loaders.

当您使用缓存加载程序时,将只有一个加载程序.所以你可以得到它并调用它的reset函数来重置模板缓存.

When you're using cached loader, there would be only one loader out there. So you can get it and calls its reset function to reset template cache.

这里是一些代码片段,我没有自己测试过.

Here are some code snippets, I haven't test it myself.

from django.template.loader import template_source_loaders
loader = template_source_loaders[0]
loader.reset()

如果你检查django.template.loaders.cached,你可以看到django 简单地使用一个变量template_cache 来保存模板名称到模板路径缓存.它不使用 memcached.所以应该在django重启时重新设置.

If you check django.template.loaders.cached, you can see that django simply use one variable template_cache to hold the template name to template path cache. It doesn't use memcached. So it should be reset when django restart.

这篇关于重置 Django 缓存模板加载器的缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆