Skip to content

Commit

Permalink
make oereblex cache thred safe
Browse files Browse the repository at this point in the history
  • Loading branch information
vvmruder committed Dec 16, 2024
1 parent b820592 commit 7135e15
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ def document_records_from_oereblex(self, params, geolink, law_status, oereblex_p
log.debug("document_records_from_oereblex() start, GEO-Link {}, law status {}, oereblex_params {}"
.format(geolink, law_status.code, oereblex_params))
identifier = '{}{}{}'.format(geolink, law_status.code, params.language)
if identifier in self._queried_geolinks:
if identifier in self._queried_geolinks[params.identifier]:
log.debug('skip querying this geolink "{}" because it was fetched already.'.format(identifier))
log.debug('use already queried instead')
else:
self._oereblex_source.read(params, geolink, law_status, oereblex_params)
log.debug("document_records_from_oereblex() returning {} records"
.format(len(self._oereblex_source.records)))
self._queried_geolinks[identifier] = self._oereblex_source.records
return self._queried_geolinks[identifier]
self._queried_geolinks[params.identifier][identifier] = self._oereblex_source.records
return self._queried_geolinks[params.identifier][identifier]

def collect_related_geometries_by_real_estate(self, session, real_estate):
"""
Expand Down Expand Up @@ -133,6 +133,10 @@ def collect_related_geometries_by_real_estate(self, session, real_estate):
).all()

def read(self, params, real_estate, bbox):
# resetting the local per request cache of queried oereblex links
self._queried_geolinks = {}
return super(DatabaseOEREBlexSource, self).read(params, real_estate, bbox)
# adding a local cache depending on the request identifier
self._queried_geolinks[params.identifier] = {}
# calling the original logic
result = super(DatabaseOEREBlexSource, self).read(params, real_estate, bbox)
# removing the cache after work is done
del self._queried_geolinks[params.identifier]
return result
4 changes: 4 additions & 0 deletions pyramid_oereb/core/views/webservice.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-

import logging
from uuid import uuid4

# import yappi
import qrcode
import io
Expand Down Expand Up @@ -637,6 +639,8 @@ def __init__(self, response_format, with_geometry=False, images=False, signed=Fa
self.__topics__ = topics
self.__extract_url__ = extract_url
self.__qr_code_ref__ = qr_code_ref
#uniquely identifier to reference the original request in the pyramid_oereb system
self.identifier = str(uuid4())

def set_identdn(self, identdn):
"""
Expand Down

0 comments on commit 7135e15

Please sign in to comment.