Skip to content

Commit 9097719

Browse files
authored
Merge pull request #309 from torchbox/support/redis-config-update
Support/redis config update
2 parents 0928ad3 + 50d53dd commit 9097719

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

docker-compose.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ services:
7676
- .:/app:ro
7777

7878
redis:
79-
image: redis:6.0
79+
image: redis:7.2
8080
expose:
8181
- 6379
8282
logging:

tbx/settings/base.py

+36-3
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,47 @@
183183
MEDIA_PREFIX = env.get("MEDIA_PREFIX", "")
184184

185185

186+
# Server-side cache settings. Do not confuse with front-end cache.
187+
# https://docs.djangoproject.com/en/stable/topics/cache/
188+
# If the server has a Redis instance exposed via a URL string in the REDIS_URL
189+
# environment variable, prefer that. Otherwise use the database backend. We
190+
# usually use Redis in production and database backend on staging and dev. In
191+
# order to use database cache backend you need to run
192+
# "django-admin createcachetable" to create a table for the cache.
193+
#
186194
# Do not use the same Redis instance for other things like Celery!
187-
if "REDIS_URL" in env:
195+
196+
# Prefer the TLS connection URL over non
197+
REDIS_URL = env.get("REDIS_TLS_URL", env.get("REDIS_URL"))
198+
199+
if REDIS_URL:
200+
connection_pool_kwargs = {}
201+
202+
if REDIS_URL.startswith("rediss"):
203+
# Heroku Redis uses self-signed certificates for secure redis conections. https://stackoverflow.com/a/66286068
204+
# When using TLS, we need to disable certificate validation checks.
205+
connection_pool_kwargs["ssl_cert_reqs"] = None
206+
207+
redis_options = {
208+
"IGNORE_EXCEPTIONS": True,
209+
"SOCKET_CONNECT_TIMEOUT": 2, # seconds
210+
"SOCKET_TIMEOUT": 2, # seconds
211+
"CONNECTION_POOL_KWARGS": connection_pool_kwargs,
212+
}
213+
188214
CACHES = {
189215
"default": {
190216
"BACKEND": "django_redis.cache.RedisCache",
191-
"LOCATION": env["REDIS_URL"],
192-
}
217+
"LOCATION": REDIS_URL + "/0",
218+
"OPTIONS": redis_options,
219+
},
220+
"renditions": {
221+
"BACKEND": "django_redis.cache.RedisCache",
222+
"LOCATION": REDIS_URL + "/1",
223+
"OPTIONS": redis_options,
224+
},
193225
}
226+
DJANGO_REDIS_LOG_IGNORED_EXCEPTIONS = True
194227
else:
195228
CACHES = {
196229
"default": {

0 commit comments

Comments
 (0)