简体   繁体   中英

Django-admin custom command don't store cache value

I've an unexpected behavior while using django command.

I'm using the standard Django cache system. My settings:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        'LOCATION': 'unique-snowflake',
        'TIMEOUT': 0,
    }
}

This is my command code:

#coding: utf-8
import os
import sys
import time

from django.core.management.base import BaseCommand
from django.core.cache import cache

class Command(BaseCommand):

    def handle(self, *args, **options):
        print(cache.get("mykey"))
        print(cache.set("mykey", "TEST", 300))

When I run it twice within the same minute I get the following output:

(venv_crypto_bot) macbook-pro:project dauzon$ python manage.py generate_data
None
None
(venv_crypto_bot) macbook-pro:project dauzon$ python manage.py generate_data
None
None

I'm on the dev server.

Clearly, Django don't store value in cache when I using a command. I don't know if it's due to I run the development server.

Cache runs correctly when I visiting the webpages of my dev server.

There are no details about the disabled cache (on command) in the Django documentation.

How can I store cache value within a Django custom command?

Problem probably is that you are running LocMemCache which is not shared through multiple process instances

Note that each process will have its own private cache instance, which means no cross-process caching is possible. This obviously also means the local memory cache isn't particularly memory-efficient, so it's probably not a good choice for production environments. It's nice for development.

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