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