From a448eaaf17bd9f2feb8b305efe2350a88e7a6bb4 Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Thu, 8 Mar 2018 23:20:13 +0100
Subject: [PATCH] Fix #117: Use django-cacheops to cache common ORM requests

---
 .gitlab-ci.yml                  |  2 ++
 api/config/settings/common.py   | 10 ++++++++++
 api/requirements/base.txt       |  1 +
 api/test.yml                    |  1 +
 changes/changelog.d/117.feature |  1 +
 5 files changed, 15 insertions(+)
 create mode 100644 changes/changelog.d/117.feature

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index e4accd722d..c6a57b0693 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -23,6 +23,8 @@ test_api:
     DJANGO_ALLOWED_HOSTS: "localhost"
     DATABASE_URL: "postgresql://postgres@postgres/postgres"
     FUNKWHALE_URL: "https://funkwhale.ci"
+    CACHEOPS_ENABLED: "false"
+
   before_script:
     - cd api
     - pip install -r requirements/base.txt
diff --git a/api/config/settings/common.py b/api/config/settings/common.py
index 1def688241..000289e515 100644
--- a/api/config/settings/common.py
+++ b/api/config/settings/common.py
@@ -60,6 +60,7 @@ THIRD_PARTY_APPS = (
     'mptt',
     'dynamic_preferences',
     'django_filters',
+    'cacheops',
 )
 
 
@@ -369,6 +370,15 @@ MUSICBRAINZ_CACHE_DURATION = env.int(
     'MUSICBRAINZ_CACHE_DURATION',
     default=300
 )
+CACHEOPS_REDIS = env('CACHE_URL', default=CACHE_DEFAULT)
+CACHEOPS_ENABLED = env.bool('CACHEOPS_ENABLED', default=True)
+CACHEOPS = {
+    'music.artist': {'ops': 'all', 'timeout': 60 * 60},
+    'music.album': {'ops': 'all', 'timeout': 60 * 60},
+    'music.track': {'ops': 'all', 'timeout': 60 * 60},
+    'music.trackfile': {'ops': 'all', 'timeout': 60 * 60},
+    'taggit.tag': {'ops': 'all', 'timeout': 60 * 60},
+}
 
 # Custom Admin URL, use {% url 'admin:index' %}
 ADMIN_URL = env('DJANGO_ADMIN_URL', default='^api/admin/')
diff --git a/api/requirements/base.txt b/api/requirements/base.txt
index d6800f3b50..8ec2517be6 100644
--- a/api/requirements/base.txt
+++ b/api/requirements/base.txt
@@ -58,3 +58,4 @@ python-magic==0.4.15
 ffmpeg-python==0.1.10
 channels>=2,<2.1
 channels_redis>=2.1,<2.2
+django-cacheops>=4,<4.1
diff --git a/api/test.yml b/api/test.yml
index 5e785cb1ac..85a3a37f0e 100644
--- a/api/test.yml
+++ b/api/test.yml
@@ -13,5 +13,6 @@ services:
       - "DJANGO_ALLOWED_HOSTS=localhost"
       - "DATABASE_URL=postgresql://postgres@postgres/postgres"
       - "FUNKWHALE_URL=https://funkwhale.test"
+      - "CACHEOPS_ENABLED=False"
   postgres:
     image: postgres
diff --git a/changes/changelog.d/117.feature b/changes/changelog.d/117.feature
new file mode 100644
index 0000000000..2df028edc5
--- /dev/null
+++ b/changes/changelog.d/117.feature
@@ -0,0 +1 @@
+Use django-cacheops to cache common ORM requests (#117)
-- 
GitLab