Django不使用memcached框架 [英] Django doesn't use memcached framework

查看:54
本文介绍了Django不使用memcached框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出Django缓存框架的工作方式.我在 settings.py 中设置了memcached,但是加载页面的时间并没有变短,并且Django-debug-toolbar显示了0个缓存调用.

I'm trying to find out how Django caching framework works. I set memcached in settings.py but the time of loading page didn't get smaller and Django-debug-toolbar shows 0 cache calls.

这是我在settings.py中设置的:

This is what I've set in settings.py:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:11211',
    }
}

CACHE_BACKEND = 'memcached://127.0.0.1:11211/'
CACHE_MIDDLEWARE_ALIAS = "default"
CACHE_MIDDLEWARE_SECONDS  = 60

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'querycount.middleware.QueryCountMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.cache.UpdateCacheMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.cache.FetchFromCacheMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'debug_toolbar.middleware.DebugToolbarMiddleware',

]

现在,我用对象表刷新了两次页面.我以为第二次不应该进行数据库查找,因为什么都没有改变.

Now I refreshed two times the page with a table of objects. I thought that the second time there should be no database lookups because nothing changed.

我想念什么?

推荐答案

您应该在视图上放置 @cache_page 装饰器以启用该视图的缓存.参见 https://docs.djangoproject.com/zh/1.10/topics/cache/#the-per-view-cache (示例)

You should put @cache_page decorator on your view to enable caching for that view. See https://docs.djangoproject.com/en/1.10/topics/cache/#the-per-view-cache for examples

这篇关于Django不使用memcached框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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