brotli: degrade gracefully when redis is missing

This commit is contained in:
codl 2021-03-12 12:12:18 +01:00
parent 785d0509af
commit f10b9dac80
1 changed files with 30 additions and 26 deletions

View File

@ -5,6 +5,7 @@ from hashlib import sha256
import redis as libredis
import os.path
import mimetypes
from redis.exceptions import RedisError
class BrotliCache(object):
@ -34,6 +35,7 @@ class BrotliCache(object):
digest = sha256(body).hexdigest()
cache_key = 'brotlicache:{}'.format(digest)
try:
encbody = self.redis.get(cache_key)
response.headers.set('brotli-cache', 'HIT')
if not encbody:
@ -60,6 +62,8 @@ class BrotliCache(object):
response.headers.set('vary', 'accept-encoding')
response.set_data(encbody)
return response
except RedisError:
response.headers.set('brotli-cache', 'ERROR')
return response