add more granular brotli cache header

This commit is contained in:
codl 2017-08-23 11:42:53 +02:00
parent 1dfb728805
commit 40fbea082f
No known key found for this signature in database
GPG Key ID: 6CD7C8891ED1233A
1 changed files with 6 additions and 1 deletions

View File

@ -7,10 +7,11 @@ import os.path
import mimetypes
class BrotliCache(object):
def __init__(self, redis_kwargs={}, max_wait=0.3, expire=60*60*12):
def __init__(self, redis_kwargs={}, max_wait=0.05, expire=60*60*12):
self.redis = redis.StrictRedis(**redis_kwargs)
self.max_wait = max_wait
self.expire = expire
self.redis.client_setname('brotlicache')
def compress(self, cache_key, lock_key, body, mode=brotli_.MODE_GENERIC):
encbody = brotli_.compress(body, mode=mode)
@ -37,6 +38,10 @@ class BrotliCache(object):
if self.max_wait > 0:
t.join(self.max_wait)
encbody = self.redis.get(cache_key)
if not encbody:
response.headers.set('x-brotli-cache', 'TIMEOUT')
else:
response.headers.set('x-brotli-cache', 'LOCKED')
if encbody:
response.headers.set('content-encoding', 'br')
response.headers.set('vary', 'accept-encoding')