def __init__(self, rc=None, use_in_memory_on_failure=True): logger.info("try to use redis as backend.") try: import redis_pubsub_dict import functools redis_pubsub_dict.dumps = lambda x: json.dumps(x) redis_pubsub_dict.loads = lambda x: Util.try_function_on_dict([ User.from_json, OAuth2Service.from_json, LoginService.from_json, OAuth2Token.from_json, Token.from_json, load_service_with_tokens, ])(x) redis_pubsub_dict.RedisDict.to_json = lambda x: dict(x.items()) redis_pubsub_dict.RedisDict.__eq__ = ( lambda x, other: dict(x.items()) == other) redis_pubsub_dict.RedisDict.keys = keys redis_pubsub_dict.RedisDict.__len__ = lambda self: self.size() # runs in RDS ecosystem, use redis as backend if rc is None: logger.debug("No redis client was given. Create one.") startup_nodes = [{ "host": os.getenv("REDIS_HOST", "localhost"), "port": os.getenv("REDIS_PORT", "6379"), }] try: logger.debug("first try cluster") from rediscluster import RedisCluster rc = RedisCluster( startup_nodes=startup_nodes, decode_responses=True, skip_full_coverage_check=True, cluster_down_retry_attempts=1, ) rc.cluster_info() # provoke an error message except Exception as e: logger.error(e) logger.debug("Cluster has an error, try standalone redis") from redis import Redis rc = Redis( **(startup_nodes[0]), db=0, decode_responses=True, ) rc.info() # provoke an error message logger.debug("set redis backed dict") self._storage = redis_pubsub_dict.RedisDict( rc, "tokenstorage_storage") self._services = redis_pubsub_dict.RedisDict( rc, "tokenstorage_services") self.__rc = rc self.__rc_helper = None try: from redis import Redis logger.debug("try to initialize the helper redis conn.") rc_helper = Redis( host="{}-master".format( os.getenv("REDIS_HELPER_HOST", "localhost")), port=os.getenv("REDIS_HELPER_PORT", "6379"), db=0, decode_responses=True, ) rc_helper.info() # provoke an error message self.__rc_helper = rc_helper self.__redis_helper_channel = os.getenv( "REDIS_CHANNEL", "TokenStorage_Refresh_Token") logger.debug("initialized helper redis conn.") except Exception as e: logger.error("cannot initialize helper redis conn.") logger.error(e, exc_info=True) logger.debug("set methods to redis backed dict to use it as list") self._services.append = append.__get__(self._services) except Exception as e: logger.error(e) logger.info("no redis found.") if not use_in_memory_on_failure: logger.info("exit...") import sys sys.exit() logger.info("use in-memory") self._storage = {} self._services = []
if os.getenv("USE_LOCAL_DICTS", "False") == "True": user_store = {} else: startup_nodes_cluster = [{ "host": os.getenv("REDIS_HOST", "localhost"), "port": os.getenv("REDIS_PORT", "6379"), }] rcCluster = RedisCluster( startup_nodes=startup_nodes_cluster, skip_full_coverage_check=True, cluster_down_retry_attempts=1, ) rcCluster.cluster_info() # provoke an error message user_store = redis_pubsub_dict.RedisDict(rcCluster, "web_userstore") # clients = redis_pubsub_dict.RedisDict(rcCluster, "web_clients") flask_config['SESSION_TYPE'] = 'redis' flask_config["SESSION_REDIS"] = rcCluster app = Flask(__name__, static_folder=os.getenv("FLASK_STATIC_FOLDER", "/usr/share/nginx/html")) ### Tracing begin ### tracer_config = { "sampler": { "type": "const", "param": 1,
def __init__(self, app, config=None, rc=None): """ Global config for flask optimize. Cache have to enabled manually per function with decorator #set_cache_timeout Args: config: global configure values """ logger.info("Initialize FlaskOptimize...") try: from redis_pubsub_dict import RedisDict # runs in RDS ecosystem, use redis as backend if rc is None: logger.debug("No redis client was given. Create one.") startup_nodes = [{ "host": os.getenv("REDIS_HOST", "localhost"), "port": os.getenv("REDIS_PORT", "6379"), }] try: logger.debug("first try cluster") from rediscluster import RedisCluster rc = RedisCluster( startup_nodes=startup_nodes, decode_responses=True, skip_full_coverage_check=True, cluster_down_retry_attempts=1, ) rc.cluster_info() # provoke an error message except Exception as e: logger.error(e) logger.debug("Cluster has an error, try standalone redis") from redis import Redis rc = Redis( **(startup_nodes[0]), db=0, decode_responses=True, ) rc.info() # provoke an error message self._redis_key = "FlaskOptimize_Caching_{}".format(self.name) self._cache = RedisDict(rc, self._redis_key) self._timestamp = RedisDict(rc, self._redis_key) except: logger.debug( "No redis client was given or found. Use local dicts.") self._cache = {} self._timestamp = {} if config is None: config = {"compress": True, "minify": True} logger.info("Optimizer config: {}".format(config)) self.config = config self.init_app(app)