diff --git a/CHANGES.rst b/CHANGES.rst index 3636b0a..6ceb57a 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,8 @@ +1.2.5 (13-10-2023) +------------------ + +- Gateway niet langer cachen (#232) + 1.2.4 (23-05-2023) ------------------ diff --git a/crabpy/gateway/adressenregister.py b/crabpy/gateway/adressenregister.py index 5b78c41..b92b22f 100644 --- a/crabpy/gateway/adressenregister.py +++ b/crabpy/gateway/adressenregister.py @@ -19,7 +19,7 @@ SHORT_CACHE = make_region() -def setup_cache(cache_settings): +def setup_cache(cache_settings, gateway): if cache_settings is None: if not LONG_CACHE.is_configured: LONG_CACHE.configure("dogpile.cache.null") @@ -31,6 +31,37 @@ def setup_cache(cache_settings): cache_settings["short.replace_existing_backend"] = True SHORT_CACHE.configure_from_config(cache_settings, "short.") + original_serializer = LONG_CACHE.serializer + original_deserializer = LONG_CACHE.deserializer + + def serializer(value): + """Remove the gateway object before serializing.""" + if isinstance(value, list): + for item in value: + if isinstance(item, GatewayObject): + item.gateway = None + else: + if isinstance(value, GatewayObject): + value.gateway = None + return original_serializer(value) + + def deserializer(value): + """Add the gateway object after deserializing.""" + result = original_deserializer(value) + if isinstance(result, list): + for item in result: + if isinstance(item, GatewayObject): + item.gateway = gateway + else: + if isinstance(result, GatewayObject): + result.gateway = gateway + return result + + LONG_CACHE.serializer = serializer if original_serializer else None + LONG_CACHE.deserializer = deserializer if original_deserializer else None + SHORT_CACHE.serializer = serializer if original_serializer else None + SHORT_CACHE.deserializer = deserializer if original_deserializer else None + def cache_on_attribute(attribute): """ @@ -154,7 +185,7 @@ def provincie_from_json_data(data): with open(os.path.join(data_dir, "gemeenten.json"), encoding="utf-8") as f: self.gemeenten = [gemeente_from_json_data(data) for data in json.load(f)] - setup_cache(cache_settings) + setup_cache(cache_settings, self) def list_gewesten(self): return self.gewesten diff --git a/setup.py b/setup.py index ebd76b9..9f303aa 100755 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="crabpy", - version="1.2.4", + version="1.2.5", description="Interact with geographical webservices by Informatie Vlaanderen.", long_description=open("README.rst").read() + "\n\n" + open("CHANGES.rst").read(), author="Onroerend Erfgoed", diff --git a/tests/__init__.py b/tests/__init__.py index 2259adf..b0cf141 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -9,7 +9,8 @@ TEST_DIR = os.path.dirname(__file__) config.read(os.path.join(TEST_DIR, "test.ini")) adressenregister.setup_cache( - {"long.backend": "dogpile.cache.null", "short.backend": "dogpile.cache.null"} + {"long.backend": "dogpile.cache.null", "short.backend": "dogpile.cache.null"}, + None ) @@ -34,10 +35,12 @@ def memory_cache(): { "long.backend": "dogpile.cache.memory", "short.backend": "dogpile.cache.memory", - } + }, + None ) yield finally: adressenregister.setup_cache( - {"long.backend": "dogpile.cache.null", "short.backend": "dogpile.cache.null"} + {"long.backend": "dogpile.cache.null", "short.backend": "dogpile.cache.null"}, + None )