|
183 | 183 | MEDIA_PREFIX = env.get("MEDIA_PREFIX", "")
|
184 | 184 |
|
185 | 185 |
|
| 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 | +# |
186 | 194 | # 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 | + |
188 | 214 | CACHES = {
|
189 | 215 | "default": {
|
190 | 216 | "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 | + }, |
193 | 225 | }
|
| 226 | + DJANGO_REDIS_LOG_IGNORED_EXCEPTIONS = True |
194 | 227 | else:
|
195 | 228 | CACHES = {
|
196 | 229 | "default": {
|
|
0 commit comments