Ejemplo n.º 1
0
    def test_memoize(self):
        def func(a, b):
            return a + b + random.randrange(0, 100000)

        config = get_cache_config('simple')
        cache = Cache(namespace=self.namespace, **config)
        cache_key1 = self.cache._memoize_make_cache_key()(func)
        cache_key2 = cache._memoize_make_cache_key()(func)
        nt.assert_equal(cache_key1, cache_key2)
Ejemplo n.º 2
0
    def test_memoize(self):
        def func(a, b):
            return a + b + random.randrange(0, 100000)

        config = get_cache_config('simple')
        cache = Cache(namespace=self.namespace, **config)
        cache_key1 = self.cache._memoize_make_cache_key()(func)
        cache_key2 = cache._memoize_make_cache_key()(func)
        nt.assert_equal(cache_key1, cache_key2)
Ejemplo n.º 3
0
    def test_timeout(self):
        config = get_cache_config('simple', CACHE_DEFAULT_TIMEOUT=1)
        self.cache = Cache(**config)

        @self.cache.memoize(50)
        def func(a, b):
            return a + b + random.randrange(0, 100000)

        result = func(5, 2)
        time.sleep(2)
        nt.assert_equal(func(5, 2), result)
Ejemplo n.º 4
0
    def test_timeout(self):
        config = get_cache_config('simple', CACHE_DEFAULT_TIMEOUT=1)
        self.cache = Cache(**config)

        @self.cache.memoize(50)
        def func(a, b):
            return a + b + random.randrange(0, 100000)

        result = func(5, 2)
        time.sleep(2)
        nt.assert_equal(func(5, 2), result)
Ejemplo n.º 5
0
def setup_func(*args, **kwargs):
    namespace = kwargs.pop('namespace', None)
    client_name = kwargs.pop('client_name', None)

    if client_name:
        CACHE_OPTIONS = kwargs.get('CACHE_OPTIONS', {})
        CACHE_OPTIONS['preferred_memcache'] = client_name
        kwargs['CACHE_OPTIONS'] = CACHE_OPTIONS

    config = get_cache_config(*args, **kwargs)
    cache = Cache(namespace=namespace, **config)
    return cache
Ejemplo n.º 6
0
def setup_func(*args, **kwargs):
    namespace = kwargs.pop('namespace', None)
    client_name = kwargs.pop('client_name', None)

    if client_name:
        CACHE_OPTIONS = kwargs.get('CACHE_OPTIONS', {})
        CACHE_OPTIONS['preferred_memcache'] = client_name
        kwargs['CACHE_OPTIONS'] = CACHE_OPTIONS

    config = get_cache_config(*args, **kwargs)
    cache = Cache(namespace=namespace, **config)
    return cache
Ejemplo n.º 7
0
def configure_cache(app):
    if app.config.get("PROD_SERVER") or app.config.get("DEBUG_MEMCACHE"):
        cache_type = get_cache_type(spread=False)
        cache_dir = None
    else:
        cache_type = "filesystem"
        parent_dir = Path(p.dirname(BASEDIR))
        cache_dir = parent_dir.joinpath(".cache", f"v{DEFAULT_PROTOCOL}")

    message = f"Set cache type to {cache_type}"
    cache_config = get_cache_config(cache_type,
                                    CACHE_DIR=cache_dir,
                                    **app.config)

    if cache_config["CACHE_TYPE"] == "filesystem":
        message += f" in {cache_config['CACHE_DIR']}"

    app.logger.debug(message)
    cache.init_app(app, config=cache_config)

    # TODO: keep until https://github.com/sh4nks/flask-caching/issues/113 is solved
    DEF_TIMEOUT = app.config.get("CACHE_DEFAULT_TIMEOUT")
    timeout = app.config.get("SET_TIMEOUT", DEF_TIMEOUT)
    cache.set = partial(cache.set, timeout=timeout)
Ejemplo n.º 8
0
def create_app(config_mode=None, config_file=None):
    app = Flask(__name__)
    app.url_map.strict_slashes = False
    cors.init_app(app)
    compress.init_app(app)
    required_setting_missing = False

    @app.before_request
    def clear_trailing():
        request_path = request.path

        if request_path != "/" and request_path.endswith("/"):
            return redirect(request_path[:-1])

    app.config.from_object(default_settings)

    if config_mode:
        app.config.from_object(getattr(config, config_mode))
    elif config_file:
        app.config.from_pyfile(config_file)
    else:
        app.config.from_envvar("APP_SETTINGS", silent=True)

    username = app.config.get("RQ_DASHBOARD_USERNAME")
    password = app.config.get("RQ_DASHBOARD_PASSWORD")
    prefix = app.config.get("API_URL_PREFIX")
    server_name = app.config.get("SERVER_NAME")

    for setting in app.config.get("REQUIRED_SETTINGS", []):
        if not app.config.get(setting):
            required_setting_missing = True
            logger.error(f"App setting {setting} is missing!")

    if app.config.get("PROD_SERVER"):
        if server_name:
            logger.info(f"SERVER_NAME is {server_name}.")
        else:
            logger.error(f"SERVER_NAME is not set!")

        for setting in app.config.get("REQUIRED_PROD_SETTINGS", []):
            if not app.config.get(setting):
                required_setting_missing = True
                logger.error(f"App setting {setting} is missing!")

    if not required_setting_missing:
        logger.info(f"All required app settings present!")

    if username and password:
        add_basic_auth(blueprint=rq, username=username, password=password)
        logger.info(f"Creating RQ-dashboard login for {username}")

    app.register_blueprint(api)
    app.register_blueprint(rq, url_prefix=f"{prefix}/dashboard")

    if app.config.get("TALISMAN"):
        from flask_talisman import Talisman

        Talisman(app)

    if app.config.get("HEROKU") or app.config.get("DEBUG_MEMCACHE"):
        cache_type = get_cache_type(spread=False)
    else:
        cache_type = "filesystem"

    if config_mode not in {"Production", "Custom", "Ngrok"}:
        app.config["ENVIRONMENT"] = "staging"

    cache_config = get_cache_config(cache_type, **app.config)

    ###########################################################################
    # TODO - remove once mezmorize PR is merged
    if cache_type == "filesystem" and not cache_config.get("CACHE_DIR"):
        cache_config["CACHE_DIR"] = path.join(
            path.abspath(path.dirname(__file__)), "cache")
    ###########################################################################

    cache.init_app(app, config=cache_config)
    return app