memcached:item在缓存中不可用

时间:2012-01-04 09:07:45

标签: django memcached

我遇到了memcached的问题。我使用带有模板'question_%d_%d'的字符串键存储值。我有记忆:

STAT bytes 13307757 
STAT limit_maxbytes 134217728

但这是我的应用程序的日志:

2012-01-03 16:40:42,896 Get question for key question_4_1045: cache miss
2012-01-03 18:03:10,270 Get question for key question_4_1045: cache miss
2012-01-03 22:26:16,454 Get question for key question_4_1045: cache miss
2012-01-04 02:01:54,639 Get question for key question_4_1045: cache miss
2012-01-04 02:45:03,647 Get question for key question_4_1045: cache miss
2012-01-04 02:46:55,880 Get question for key question_4_1045: cache hit
2012-01-04 02:51:55,606 Get question for key question_4_1045: cache miss

所以我们可以看到,使用相同密钥的两个顺序调用导致两次缓存未命中,并且只从缓存中获取一次值。

为什么memcached会从缓存中删除我的数据,即使有足够的空间?有可能解决它吗?

我试图检查我的memcached日志文件(根据配置文件是/var/log/memcached.log),但它是空的。 谢谢!

UPD: Django缓存设置:

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

CACHE_BACKEND = 'memcached://127.0.0.1:11211/'

吸气剂:

from django.core.cache import cache    
def get_question(level, random_num):
        key = 'question_' + unicode(level) + '_' + unicode(random_num)
        question = cache.get(key)
        if question is None:
            question = Question.objects.filter(level=level).order_by('id')[random_num]
            cache.set(key, question)
            log_message('Get question for key %s: cache miss' % key)
        else:
            log_message('Get question for key %s: cache hit' % key)
        return question

1 个答案:

答案 0 :(得分:0)

经过一番调查后,我看到了问题的根本原因。 我在settings.py中使用了Django 1.2的缓存配置(我使用django 1.2):

CACHE_BACKEND = 'memcached://127.0.0.1:11211/'

当我决定更改超时时,我从Django 1.3的新Django文档中复制粘贴的超时设置。因此,这些超时设置被忽略,并使用了默认超时(5分钟)。

相关问题