From 47033890a5062532c778cdbf9ed8aae45f8e8ed1 Mon Sep 17 00:00:00 2001
From: Parbhat Puri <parbhatpuri17@gmail.com>
Date: Mon, 15 Apr 2019 10:26:53 +0000
Subject: [PATCH] Cache public site pages and Apply homepage

---
 opentech/apply/home/models.py          |  6 +++++-
 opentech/public/utils/models.py        |  4 ++++
 opentech/public/utils/wagtail_hooks.py |  9 +++++++++
 opentech/settings/base.py              | 14 ++++++++++++++
 opentech/settings/dev.py               |  2 ++
 opentech/settings/local.py.example     |  3 +++
 requirements.txt                       |  1 +
 7 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/opentech/apply/home/models.py b/opentech/apply/home/models.py
index 31a7e55b6..934f42516 100644
--- a/opentech/apply/home/models.py
+++ b/opentech/apply/home/models.py
@@ -1,12 +1,16 @@
+from django.db import models
+from django.utils.decorators import method_decorator
+
 from wagtail.admin.edit_handlers import FieldPanel
 from wagtail.core.models import Page
 from wagtail.search import index
 
-from django.db import models
+from wagtailcache.cache import cache_page
 
 from opentech.apply.funds.models import ApplicationBase, LabBase
 
 
+@method_decorator(cache_page, name='serve')
 class ApplyHomePage(Page):
     # Only allow creating HomePages at the root level
     parent_page_types = ['wagtailcore.Page']
diff --git a/opentech/public/utils/models.py b/opentech/public/utils/models.py
index d9f398dae..d75eeb191 100644
--- a/opentech/public/utils/models.py
+++ b/opentech/public/utils/models.py
@@ -1,5 +1,6 @@
 from django.core.exceptions import ValidationError
 from django.db import models
+from django.utils.decorators import method_decorator
 
 from wagtail.admin.edit_handlers import (
     FieldPanel,
@@ -16,6 +17,8 @@ from wagtail.images.edit_handlers import ImageChooserPanel
 from wagtail.snippets.models import register_snippet
 from wagtail.contrib.settings.models import BaseSetting, register_setting
 
+from wagtailcache.cache import cache_page
+
 
 class LinkFields(models.Model):
     """
@@ -242,6 +245,7 @@ class SystemMessagesSettings(BaseSetting):
     ]
 
 
+@method_decorator(cache_page, name='serve')
 class BasePage(SocialFields, ListingFields, Page):
     show_in_menus_default = True
 
diff --git a/opentech/public/utils/wagtail_hooks.py b/opentech/public/utils/wagtail_hooks.py
index bb0193b91..0efa847d6 100644
--- a/opentech/public/utils/wagtail_hooks.py
+++ b/opentech/public/utils/wagtail_hooks.py
@@ -3,6 +3,8 @@ from django.contrib.staticfiles.templatetags.staticfiles import static
 from wagtail.contrib.modeladmin.options import ModelAdminGroup, ModelAdmin, modeladmin_register
 from wagtail.core import hooks
 
+from wagtailcache.cache import clear_cache
+
 from opentech.public.news.models import NewsType
 from opentech.public.people.models import PersonType
 
@@ -31,3 +33,10 @@ def editor_css():
     link = '<link rel="stylesheet" href="{}">\n'
     path = static('css/apply/wagtail_editor.css')
     return link.format(path)
+
+
+@hooks.register('after_create_page')
+@hooks.register('after_edit_page')
+def clear_wagtailcache(request, page):
+    if page.live:
+        clear_cache()
diff --git a/opentech/settings/base.py b/opentech/settings/base.py
index 06bddd79a..56bebca01 100644
--- a/opentech/settings/base.py
+++ b/opentech/settings/base.py
@@ -118,6 +118,7 @@ INSTALLED_APPS = [
     'django_fsm',
     'django_pwned_passwords',
     'rest_framework',
+    'wagtailcache',
 
     'hijack',
     'compat',
@@ -202,6 +203,12 @@ if 'REDIS_URL' in env:
         "default": {
             "BACKEND": "django_redis.cache.RedisCache",
             "LOCATION": env['REDIS_URL'],
+        },
+        "wagtailcache": {
+            "BACKEND": "wagtailcache.compat_backends.django_redis.RedisCache",
+            "LOCATION": env['REDIS_URL'],
+            'KEY_PREFIX': 'wagtailcache',
+            'TIMEOUT': 3600,
         }
     }
 else:
@@ -209,9 +216,16 @@ else:
         'default': {
             'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
             'LOCATION': 'database_cache',
+        },
+        'wagtailcache': {
+            'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
+            'LOCATION': 'database_cache',
+            'KEY_PREFIX': 'wagtailcache',
+            'TIMEOUT': 3600,
         }
     }
 
+WAGTAIL_CACHE_BACKEND = 'wagtailcache'
 
 # Set s-max-age header that is used by reverse proxy/front end cache. See
 # urls.py
diff --git a/opentech/settings/dev.py b/opentech/settings/dev.py
index 9c0c27e98..c41bae6be 100644
--- a/opentech/settings/dev.py
+++ b/opentech/settings/dev.py
@@ -6,6 +6,8 @@ DEBUG = True
 # SECURITY WARNING: keep the secret key used in production secret!
 SECRET_KEY = 'CHANGEME!!!'
 
+WAGTAIL_CACHE = False
+
 INTERNAL_IPS = ('127.0.0.1', '10.0.2.2')
 
 ALLOWED_HOSTS = ['apply.localhost', 'localhost', '127.0.0.1', 'dev.otf.is', 'dev-apply.otf.is']
diff --git a/opentech/settings/local.py.example b/opentech/settings/local.py.example
index f9ae5091d..19e16b1ee 100644
--- a/opentech/settings/local.py.example
+++ b/opentech/settings/local.py.example
@@ -22,3 +22,6 @@ CACHES = {
 # BASE_URL = 'http://otf.test'
 
 # SECRET_KEY = 'CHANGEME!!!'
+
+# Enable Wagtail Cache while developing
+# WAGTAIL_CACHE = True
\ No newline at end of file
diff --git a/requirements.txt b/requirements.txt
index ec0a4b7e3..192f6e09a 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -44,4 +44,5 @@ Pillow==4.3.0
 psycopg2==2.7.3.1
 social_auth_app_django==3.1.0
 wagtail~=2.2.0
+wagtail-cache==0.5.1
 whitenoise==4.0
-- 
GitLab