Django cache - How to set Filesystem cache in django.



In file system caching the cache files will be cached in a file/folder location. To set a file system cache the below are the steps,

1) Make the below changes in settings.py file, in the below change the location mentions the position were the changes are intended to be stored. Here ensure the location is absolute, Since the location needs to be absolute it must be starting from a root directory.

CACHES = { 'default': {
                   'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
                   'LOCATION': 'D:\sample',}}

2) Add the highlighted components to the middleware in the same order.

             MIDDLEWARE = [
             'django.middleware.security.SecurityMiddleware',
             'django.contrib.sessions.middleware.SessionMiddleware',
             'django.middleware.cache.UpdateCacheMiddleware',
             'django.middleware.common.CommonMiddleware',
             'django.middleware.cache.FetchFromCacheMiddleware',
             'django.middleware.csrf.CsrfViewMiddleware',
             'django.contrib.auth.middleware.AuthenticationMiddleware',
             'django.contrib.messages.middleware.MessageMiddleware',
             'django.middleware.clickjacking.XFrameOptionsMiddleware',]

3) Now accessing the website will trigger the cache files to be cached in the mentioned location.

output:



Comments