0

In my Django project I want to stop the cache from clearing whenever the server is restarted. Such as when I edit my views.py file, the cache is cleared and I get logged out which makes editing any features exclusive to logged in users a pain.

settings.py

SESSION_ENGINE = "django.contrib.sessions.backends.cache"

What configurations would stop cache clearing on server refresh?

3 Answers 3

0

You can switch to a different cache backend that persists data to disk or a remote cache server.

For example, you could use the "filebased" cache backend, which stores cached data on disk. To configure this backend, add the following to your settings.py file:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
        'LOCATION': '/var/tmp/django_cache',
    }
}

This will create a cache directory at /var/tmp/django_cache and store cached data there. The cache will persist across server restarts, so you won't lose your cached data when you edit your views.py file.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use a persistent cache setting to keep cache like the built in FileBasedCache, this way you can keep it when the server is restart, e.g:

django.core.cache.backends.filebased.FileBasedCache

Another option would be to use another - not built in - cache backend like Redis.

Comments

0

Go for Redis cache. You would learn how to use another great tool. Although Redis stores data typically in memory, it also provides persistence options to save data on disk periodically or on certain event. In this case, Redis loads data from the persisted data source.

You will just need to install the django-redis package and configure the cache settings in settings.py:

# settings.py
CACHES = {
    'default': {
        'BACKEND': 'django_redis.cache.RedisCache',
        'LOCATION': 'redis://127.0.0.1:6379/1',
        'OPTIONS': {
            'CLIENT_CLASS': 'django_redis.client.DefaultClient',
        }
    }
}

SESSION_ENGINE = "django.contrib.sessions.backends.cache"

If you have Redis running in port 6379 (typical port for Redis to run), both default cache and session storages will be stored in Redis.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.