From ed0369da6ff79d87102453c7323d828f4edaf319 Mon Sep 17 00:00:00 2001 From: LittleBigProgramming <20643587+LittleBigProgramming@users.noreply.github.com> Date: Thu, 27 Oct 2022 19:58:15 +0000 Subject: [PATCH] Use database caching per site for an hour using timeout of 3600 on non development environments which will use Dummy Caching (https://docs.djangoproject.com/en/4.1/topics/cache/#dummy-caching-for-development) Site wide cache will stored in cache_table, to run this initially the command of `python manage.py createcachetable` will need to be run after the deployment of this feature. --- codethesaurus/settings.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/codethesaurus/settings.py b/codethesaurus/settings.py index ab498e543..8a7bd67c1 100644 --- a/codethesaurus/settings.py +++ b/codethesaurus/settings.py @@ -51,11 +51,13 @@ MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.cache.UpdateCacheMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', - 'django.middleware.clickjacking.XFrameOptionsMiddleware' + 'django.middleware.clickjacking.XFrameOptionsMiddleware', + 'django.middleware.cache.FetchFromCacheMiddleware' ] ROOT_URLCONF = 'codethesaurus.urls' @@ -147,5 +149,20 @@ os.path.join(BASE_DIR, 'static'), ] +if os.getenv('DJANGO_DEVELOPMENT') == 'true': + CACHES = { + 'default': { + 'BACKEND': 'django.core.cache.backends.dummy.DummyCache', + } + } +else: + CACHES = { + 'default': { + 'BACKEND': 'django.core.cache.backends.db.DatabaseCache', + 'LOCATION': 'cache_table', + 'TIMEOUT': 3600 + } + } + # Configure Django App for Heroku. django_on_heroku.settings(locals(), test_runner=False, databases=False, staticfiles=True, logging=True)