简体   繁体   中英

Django shell doesn't respect cache configuration

In my settings.py I have:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
        'LOCATION': 'ws_cache_table',
        'TIMEOUT': '3000000',
        'OPTIONS': {
            'MAX_ENTRIES': 10000000
        }
    }
}

But if I do this in python manage.py shell :

from django.core.cache import cache
print type(cache)

I'm getting:

django.core.cache.backends.locmem.LocMemCache

Why!??? Now I can't clear my cache...

To prove my configuration is corect I can do:

from django.conf import settings
conf = settings.CACHES.get('default', None)

And I'm getting:

{'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
 'LOCATION': 'ws_cache_table',
 'OPTIONS': {'MAX_ENTRIES': 10000000},
 'TIMEOUT': '3000000'}

It looks like get_cache method is called before CACHES is defined...

First of all you should keep in mind that your local_settings.py would overwrite the settings.py .

Then you should watch out what cache daemon as backend is running, as there are different ones and depending what you run you need the respective specified option.

eg for memcached the local_settings.py would read:

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

whereas for locmem:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'
        'LOCATION': '127.0.0.1:11211'
        'TIMEOUT': 3600'
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM