2016-11-10 11:50:49 +00:00
|
|
|
from flask.ext.redis import FlaskRedis
|
2016-11-11 16:47:52 +00:00
|
|
|
from flask import current_app
|
2016-11-10 11:50:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class RedisClient:
|
|
|
|
|
redis_store = FlaskRedis()
|
2016-11-11 16:47:52 +00:00
|
|
|
active = False
|
2016-11-10 11:50:49 +00:00
|
|
|
|
|
|
|
|
def init_app(self, app):
|
|
|
|
|
self.active = app.config.get('REDIS_ENABLED')
|
|
|
|
|
|
|
|
|
|
if self.active:
|
|
|
|
|
self.redis_store.init_app(app)
|
|
|
|
|
|
2016-11-11 16:47:52 +00:00
|
|
|
def set(self, key, value, ex=None, px=None, nx=False, xx=False, raise_exception=False):
|
|
|
|
|
if self.active:
|
|
|
|
|
try:
|
|
|
|
|
self.redis_store.set(key, value, ex, px, nx, xx)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
current_app.logger.exception(e)
|
|
|
|
|
if raise_exception:
|
|
|
|
|
raise e
|
|
|
|
|
|
|
|
|
|
def inc(self, key, raise_exception=False):
|
2016-11-10 11:50:49 +00:00
|
|
|
if self.active:
|
2016-11-11 16:47:52 +00:00
|
|
|
try:
|
|
|
|
|
return self.redis_store.inc(key)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
current_app.logger.exception(e)
|
|
|
|
|
if raise_exception:
|
|
|
|
|
raise e
|
2016-11-10 11:50:49 +00:00
|
|
|
|
2016-11-11 16:47:52 +00:00
|
|
|
def get(self, key, raise_exception=False):
|
2016-11-10 11:50:49 +00:00
|
|
|
if self.active:
|
2016-11-11 16:47:52 +00:00
|
|
|
try:
|
|
|
|
|
return self.redis_store.get(key)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
current_app.logger.exception(e)
|
|
|
|
|
if raise_exception:
|
|
|
|
|
raise e
|
|
|
|
|
return None
|