diff --git a/indexd/app.py b/indexd/app.py index dacc3542e..b0bfd101f 100644 --- a/indexd/app.py +++ b/indexd/app.py @@ -1,4 +1,5 @@ import flask +from urllib.parse import urlparse from .bulk.blueprint import blueprint as indexd_bulk_blueprint from .index.blueprint import blueprint as indexd_index_blueprint from .alias.blueprint import blueprint as indexd_alias_blueprint @@ -48,5 +49,20 @@ def get_app(settings=None): pass app_init(app, settings) + _setup_redis_client(app) return app + + +def _setup_redis_client(app): + """ + Sets up the redis client based on config + """ + redis_url_parts = urlparse(app.config["REDIS_HOST"]) + ssl = redis_url_parts.scheme == "https" + app.redis_client = redis.Redis( + host=redis_url_parts.netloc, + port=app.config["REDIS_PORT"], + db=app.config["REDIS_DB"], + ssl=ssl, + ) diff --git a/indexd/default_settings.py b/indexd/default_settings.py index 3eed3fdb5..032c1e405 100644 --- a/indexd/default_settings.py +++ b/indexd/default_settings.py @@ -57,4 +57,8 @@ AUTH = SQLAlchemyAuthDriver("sqlite:///auth.sq3") +CONFIG["REDIS_HOST"]: "http://redis-service" +CONFIG["REDIS_PORT"]: "6379" +CONFIG["REDIS_DB"]: 0 + settings = {"config": CONFIG, "auth": AUTH} diff --git a/indexd/index/blueprint.py b/indexd/index/blueprint.py index 650167748..9c2beb49b 100644 --- a/indexd/index/blueprint.py +++ b/indexd/index/blueprint.py @@ -220,9 +220,19 @@ def get_index_record(record): Returns a record. """ - ret = blueprint.index_driver.get(record) + # check redis + json_record = flask.current_app.redis_client.get(record) + if json_record: + json_record = json_record.loads(json_record) + else: + # get from db + ret = blueprint.index_driver.get(record) + json_record = flask.jsonify(ret) - return flask.jsonify(ret), 200 + # update redis + flask.current_app.redis_client.set(record, json.dumps(json_record)) + + return json_record, 200 @blueprint.route("/index/", methods=["POST"]) @@ -311,6 +321,9 @@ def put_index_blank_record(record): ) ret = {"did": did, "rev": rev, "baseid": baseid} + # invalidate redis for this did + flask.current_app.redis_client.delete(did) + return flask.jsonify(ret), 200 @@ -330,6 +343,9 @@ def put_index_record(record): ret = {"did": did, "baseid": baseid, "rev": rev} + # invalidate redis for this did + flask.current_app.redis_client.delete(did) + return flask.jsonify(ret), 200 @@ -345,6 +361,9 @@ def delete_index_record(record): # authorize done in delete blueprint.index_driver.delete(record, rev) + # invalidate redis for this did + flask.current_app.redis_client.delete(record) + return "", 200 @@ -388,6 +407,9 @@ def add_index_record_version(record): ret = {"did": did, "baseid": baseid, "rev": rev} + # invalidate redis for this did + flask.current_app.redis_client.delete(did) + return flask.jsonify(ret), 200 @@ -425,6 +447,10 @@ def append_aliases(record): aliases = blueprint.index_driver.get_aliases_for_did(record) aliases_payload = {"aliases": [{"value": alias} for alias in aliases]} + + # invalidate redis for this did + flask.current_app.redis_client.delete(record) + return flask.jsonify(aliases_payload), 200 @@ -448,6 +474,10 @@ def replace_aliases(record): blueprint.index_driver.replace_aliases_for_did(aliases, record) aliases_payload = {"aliases": [{"value": alias} for alias in aliases]} + + # invalidate redis for this did + flask.current_app.redis_client.delete(record) + return flask.jsonify(aliases_payload), 200 @@ -456,6 +486,9 @@ def delete_all_aliases(record): # authorization and error handling done in driver blueprint.index_driver.delete_all_aliases_for_did(record) + # invalidate redis for this did + flask.current_app.redis_client.delete(record) + return flask.jsonify("Aliases deleted successfully"), 200 @@ -464,6 +497,9 @@ def delete_one_alias(record, alias): # authorization and error handling done in driver blueprint.index_driver.delete_one_alias_for_did(alias, record) + # invalidate redis for this did + flask.current_app.redis_client.delete(record) + return flask.jsonify("Aliases deleted successfully"), 200 diff --git a/tests/default_test_settings.py b/tests/default_test_settings.py index 4579d6716..712924156 100644 --- a/tests/default_test_settings.py +++ b/tests/default_test_settings.py @@ -20,6 +20,10 @@ os.environ["PRESIGNED_FENCE_URL"] = "https://fictitious-commons.io/" os.environ["HOSTNAME"] = "fictitious-commons.io" +CONFIG["REDIS_HOST"]: "http://redis-service" +CONFIG["REDIS_PORT"]: "6379" +CONFIG["REDIS_DB"]: 0 + settings = {"config": CONFIG, "auth": AUTH} settings["config"]["TEST_DB"] = "postgres://postgres@localhost/test_migration_db"