سؤال

In the according to the docs it effectively says that you should use a KEY_PREFIX when sharing a cache instance between servers. My question is when does is at what point does the KEY_PREFIX apply? Using MemcachedStats here is basic example

from memcached_stats import MemcachedStats
from django.core.cache import get_cache

cache = get_cache('default')
assert len(cache._servers) == 1
mem = MemcachedStats(*cache._servers[0].split(":"))

# Now lets play verify no key
cache.get("TEST") == None
key = next((x for x in mem.keys() if "TEST" in x))

# Create a key
cache.set("TEST", "X", 30)
key = next((x for x in mem.keys() if "TEST" in x))

print key
':1:TEST'

At this point it looks OK - I mean the prefix is set or so I think..

from django.conf import settings
print settings.KEY_PREFIX
'beta'
print settings.SITE_ID
2
print settings.CACHE_MIDDLEWARE_KEY_PREFIX
'beta'

At this point is this just a bug?

هل كانت مفيدة؟

المحلول

Interesting problem. Turns out you need to look very closely at the documentation and notice that KEY_PREFIX is a subkey in the CACHES[<cache>]. You need to define it like this.

CACHE_MIDDLEWARE_KEY_PREFIX  = 'staging'
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': 'production_cache_server:11211',
        'KEY_PREFIX': CACHE_MIDDLEWARE_KEY_PREFIX,
    }
}

This is also the way to define a KEY_FUNCTION as well. I verified this will also work.

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': 'production.jxycyn.cfg.usw1.cache.amazonaws.com:11211',
        'KEY_FUNCTION': 'apps.core.cache_utils.make_key',
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top