Skip to content

Commit ca06f8b

Browse files
committed
Use django-cacheops to speed up serving docs and support DB going down
`django-cacheops` allows us to cache querysets into a redis backend in a granular way. We can define which models we want to cache and we can specify which instances (el proxito, web, workers, etc) will use this cache and which of them will invalidate it. This initial approach configure El Proxito to make usage of this cache when performing queries and configure Web/Celery/Build instances only to invalidate the cache when a model changed. This setup allows us to serve docs faster but also keep serving docs during `CACHEOPS_TIMEOUT` seconds even when the DB goes down. Because the way it's configured now (web is not using the cache), when the DB goes down, the footer API call will fail and the flyout menu won't be replaced properly. We could change this if we want by performing Manual caching on that view or by enabling cacheops in Web instance as well.
1 parent cf5fc00 commit ca06f8b

File tree

5 files changed

+54
-0
lines changed

5 files changed

+54
-0
lines changed

docker-compose.yml

+3
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ services:
126126
- storage
127127
command: ["/usr/local/bin/create-containers.sh"]
128128

129+
cacheops:
130+
image: redis:3.2.7
131+
129132
cache:
130133
image: redis:3.2.7
131134

docker/settings/proxito.py

+23
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@
44
class ProxitoDevSettings(DockerBaseSettings):
55
ROOT_URLCONF = 'readthedocs.proxito.urls'
66

7+
CACHEOPS_TIMEOUT = 60 * 60
8+
CACHEOPS_OPS = {'get', 'fetch'}
9+
CACHEOPS = {
10+
# readthedocs.projects.*
11+
'projects.project': {
12+
'ops': CACHEOPS_OPS,
13+
'timeout': CACHEOPS_TIMEOUT,
14+
},
15+
'projects.projectrelationship': {
16+
'ops': CACHEOPS_OPS,
17+
'timeout': CACHEOPS_TIMEOUT,
18+
},
19+
'projects.domain': {
20+
'ops': CACHEOPS_OPS,
21+
'timeout': CACHEOPS_TIMEOUT,
22+
},
23+
24+
# readthedocs.builds.*
25+
'builds.version': {
26+
'ops': CACHEOPS_OPS,
27+
'timeout': CACHEOPS_TIMEOUT,
28+
},
29+
}
730
@property
831
def MIDDLEWARE(self): # noqa
932
classes = list(super().MIDDLEWARE)

readthedocs/settings/base.py

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ def INSTALLED_APPS(self): # noqa
120120
'django_elasticsearch_dsl',
121121
'django_filters',
122122
'polymorphic',
123+
'cacheops',
123124

124125
# our apps
125126
'readthedocs.projects',

readthedocs/settings/docker.py

+25
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,31 @@ def DATABASES(self): # noqa
6666
}
6767
}
6868

69+
CACHEOPS_REDIS = 'redis://cacheops:6379/0'
70+
CACHEOPS_TIMEOUT = 60 * 60
71+
CACHEOPS = {
72+
# readthedocs.projects.*
73+
'projects.project': {
74+
'ops': (),
75+
'timeout': CACHEOPS_TIMEOUT,
76+
},
77+
'projects.projectrelationship': {
78+
'ops': (),
79+
'timeout': CACHEOPS_TIMEOUT,
80+
},
81+
'projects.domain': {
82+
'ops': (),
83+
'timeout': CACHEOPS_TIMEOUT,
84+
},
85+
86+
# readthedocs.builds.*
87+
'builds.version': {
88+
'ops': (),
89+
'timeout': CACHEOPS_TIMEOUT,
90+
},
91+
}
92+
CACHEOPS_DEGRADE_ON_FAILURE = True
93+
6994
BROKER_URL = "redis://cache:6379/0"
7095
CELERY_RESULT_BACKEND = "redis://cache:6379/0"
7196
CELERY_RESULT_SERIALIZER = "json"

requirements/pip.txt

+2
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,5 @@ django-storages==1.7.1 # pyup: <1.7.2
109109

110110
# Required only in development and linting
111111
django-debug-toolbar==2.0
112+
113+
django-cacheops==4.2

0 commit comments

Comments
 (0)