cleaner redis isolation
This commit is contained in:
parent
520560ce79
commit
b69f5db0dd
|
@ -2,25 +2,31 @@ import brotli as brotli_
|
||||||
from flask import request, make_response
|
from flask import request, make_response
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
from hashlib import sha256
|
from hashlib import sha256
|
||||||
import redis
|
import redis as libredis
|
||||||
import os.path
|
import os.path
|
||||||
import mimetypes
|
import mimetypes
|
||||||
|
|
||||||
|
|
||||||
class BrotliCache(object):
|
class BrotliCache(object):
|
||||||
def __init__(self, redis_uri='redis://', timeout=0.100, expire=60*60*6):
|
def __init__(self, redis_uri='redis://', timeout=0.100, expire=60*60*6):
|
||||||
self.redis = redis.StrictRedis.from_url(redis_uri)
|
self._redis = None
|
||||||
|
self._redis_uri = redis_uri
|
||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
self.expire = expire
|
self.expire = expire
|
||||||
|
|
||||||
|
@property
|
||||||
|
def redis(self):
|
||||||
|
if not self._redis:
|
||||||
|
self._redis = libredis.StrictRedis.from_url(self._redis_uri)
|
||||||
|
self._redis.client_setname('brotlicache')
|
||||||
|
return self._redis
|
||||||
|
|
||||||
def compress_and_cache(self, cache_key, lock_key, body, mode=brotli_.MODE_GENERIC):
|
def compress_and_cache(self, cache_key, lock_key, body, mode=brotli_.MODE_GENERIC):
|
||||||
self.redis.client_setname('brotlicache')
|
|
||||||
encbody = brotli_.compress(body, mode=mode)
|
encbody = brotli_.compress(body, mode=mode)
|
||||||
self.redis.set(cache_key, encbody, px=int(self.expire*1000))
|
self.redis.set(cache_key, encbody, px=int(self.expire*1000))
|
||||||
self.redis.delete(lock_key)
|
self.redis.delete(lock_key)
|
||||||
|
|
||||||
def wrap_response(self, response):
|
def wrap_response(self, response):
|
||||||
self.redis.client_setname('brotlicache')
|
|
||||||
if 'br' not in request.accept_encodings or response.is_streamed:
|
if 'br' not in request.accept_encodings or response.is_streamed:
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import requests
|
import requests
|
||||||
import threading
|
import threading
|
||||||
import redis
|
import redis as libredis
|
||||||
from flask import make_response, abort
|
from flask import make_response, abort
|
||||||
import secrets
|
import secrets
|
||||||
import hmac
|
import hmac
|
||||||
|
@ -12,19 +12,26 @@ import re
|
||||||
class ImgProxyCache(object):
|
class ImgProxyCache(object):
|
||||||
def __init__(self, redis_uri='redis://', timeout=10, expire=60*60,
|
def __init__(self, redis_uri='redis://', timeout=10, expire=60*60,
|
||||||
prefix='img_proxy', hmac_hash='sha1'):
|
prefix='img_proxy', hmac_hash='sha1'):
|
||||||
self.redis = redis.StrictRedis.from_url(redis_uri)
|
self._redis = None
|
||||||
|
self._redis_uri = redis_uri
|
||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
self.expire = expire
|
self.expire = expire
|
||||||
self.prefix = prefix
|
self.prefix = prefix
|
||||||
self.hash = hmac_hash
|
self.hash = hmac_hash
|
||||||
self.hmac_key = None
|
self.hmac_key = None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def redis(self):
|
||||||
|
if not self._redis:
|
||||||
|
self._redis = libredis.StrictRedis.from_url(self._redis_uri)
|
||||||
|
self._redis.client_setname('img_proxy')
|
||||||
|
return self._redis
|
||||||
|
|
||||||
def key(self, *args):
|
def key(self, *args):
|
||||||
return '{prefix}:1:{args}'.format(
|
return '{prefix}:1:{args}'.format(
|
||||||
prefix=self.prefix, args=":".join(args))
|
prefix=self.prefix, args=":".join(args))
|
||||||
|
|
||||||
def token(self):
|
def token(self):
|
||||||
self.redis.client_setname('img_proxy')
|
|
||||||
if not self.hmac_key:
|
if not self.hmac_key:
|
||||||
t = self.redis.get(self.key('hmac_key'))
|
t = self.redis.get(self.key('hmac_key'))
|
||||||
if not t:
|
if not t:
|
||||||
|
@ -54,7 +61,6 @@ class ImgProxyCache(object):
|
||||||
return url
|
return url
|
||||||
|
|
||||||
def fetch_and_cache(self, url):
|
def fetch_and_cache(self, url):
|
||||||
self.redis.client_setname('img_proxy')
|
|
||||||
resp = requests.get(url)
|
resp = requests.get(url)
|
||||||
if(resp.status_code != 200):
|
if(resp.status_code != 200):
|
||||||
return
|
return
|
||||||
|
@ -84,7 +90,6 @@ class ImgProxyCache(object):
|
||||||
resp.content, px=expire*1000)
|
resp.content, px=expire*1000)
|
||||||
|
|
||||||
def respond(self, identifier):
|
def respond(self, identifier):
|
||||||
self.redis.client_setname('img_proxy')
|
|
||||||
url = self.url_for(identifier)
|
url = self.url_for(identifier)
|
||||||
if not url:
|
if not url:
|
||||||
return abort(403)
|
return abort(403)
|
||||||
|
|
Loading…
Reference in New Issue