From fb2009590a028f3f31deb9f4337b51775fc5b143 Mon Sep 17 00:00:00 2001 From: Matthew Richards Date: Mon, 15 Mar 2021 12:37:17 +0000 Subject: [PATCH] #209: Add client cache size as a configurable option --- config.json.example | 1 + datagateway_api/common/config.py | 6 ++++++ datagateway_api/common/icat/helpers.py | 2 +- test/test_config.py | 10 ++++++++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/config.json.example b/config.json.example index d44534ec..deae7156 100644 --- a/config.json.example +++ b/config.json.example @@ -1,5 +1,6 @@ { "backend": "db", + "client_cache_size": 28, "DB_URL": "mysql+pymysql://icatdbuser:icatdbuserpw@localhost:3306/icatdb", "ICAT_URL": "https://localhost:8181", "icat_check_cert": false, diff --git a/datagateway_api/common/config.py b/datagateway_api/common/config.py index 9b5090a4..d45507c7 100644 --- a/datagateway_api/common/config.py +++ b/datagateway_api/common/config.py @@ -34,6 +34,12 @@ def set_backend_type(self, backend_type): """ self.config["backend"] = backend_type + def get_client_cache_size(self): + try: + return self.config["client_cache_size"] + except KeyError: + sys.exit("Missing config value, client_cache_size") + def get_db_url(self): try: return self.config["DB_URL"] diff --git a/datagateway_api/common/icat/helpers.py b/datagateway_api/common/icat/helpers.py index 7cc5bb1d..018be84e 100644 --- a/datagateway_api/common/icat/helpers.py +++ b/datagateway_api/common/icat/helpers.py @@ -72,7 +72,7 @@ def wrapper_requires_session(*args, **kwargs): return wrapper_requires_session -@lru_cache(maxsize=28) +@lru_cache(maxsize=config.get_client_cache_size()) def get_cached_client(session_id): """ TODO - Add docstring diff --git a/test/test_config.py b/test/test_config.py index 4d5da769..a3a4a910 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -30,6 +30,16 @@ def test_invalid_backend_type(self, invalid_config): invalid_config.get_backend_type() +class TestGetClientCacheSize: + def test_valid_client_cache_size(self, valid_config): + cache_size = valid_config.get_client_cache_size() + assert cache_size == 28 + + def test_invalid_client_cache_size(self, invalid_config): + with pytest.raises(SystemExit): + invalid_config.get_client_cache_size() + + class TestGetDBURL: def test_valid_db_url(self, valid_config): db_url = valid_config.get_db_url()