1

我在 django 的项目中缓存了一个奇怪的问题。

我可以在 django-admin 中编辑我的页面内容。当我这样做并刷新站点时-什么都没有发生。我必须等待几分钟才能进行更改。有趣的是,当我更改浏览器(或计算机)时 - 我不必等待 - 更改已开启。是django,浏览器的问题还是什么?是否可以设置 setting.py 以立即获得更改?

顺便说一句,我已经发现当我关闭“django.middleware.cache.FetchFromCacheMiddleware”时 - 问题消失了,但我不想关闭缓存......

有任何想法吗?

4

1 回答 1

1

When you have cache middleware enabled, Django caches the rendered template. When the next request is made, Django checks the cache for the template, and if it exists, it returns a 304 Not Modified rather than the normal 200 Found HTTP response. That tells your browser to pull it from its cache instead of pulling it down from the server again. (It's a lot more complicated than this in practice, but I'm simplifying).

Long and short, is this is how caching in Django works. If you don't want this behavior, then you can either disable the cache (not the cache middleware), by telling it to use the DummyCache backend:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
    }
}

This will result in Django always returning a 200 with a fresh copy because it can never find the cached copy. You can also add the following setting to settings.py:

CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True

Then, if you're logged in (through admin or otherwise), the response will vary on that and always return a fresh copy. However, this will effect normal users of your site as well, if you have any public-facing login capability.

于 2012-08-09T14:47:10.330 回答