Skip to content

Commit

Permalink
Merge pull request #235 from OnroerendErfgoed/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
claeyswo authored Oct 13, 2023
2 parents 985746f + 3754df5 commit 5f6501c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
1.2.5 (13-10-2023)
------------------

- Gateway niet langer cachen (#232)

1.2.4 (23-05-2023)
------------------

Expand Down
35 changes: 33 additions & 2 deletions crabpy/gateway/adressenregister.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 6 additions & 3 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)


Expand All @@ -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
)

0 comments on commit 5f6501c

Please sign in to comment.