Skip to content

Commit

Permalink
Parse the WMS capabilities less times.
Browse files Browse the repository at this point in the history
One time per process and OGC server,
Not one time per layers.

And use broadcast to do it on all gunicorn process/host
  • Loading branch information
sbrunner committed Jun 13, 2018
1 parent be3a30f commit 9c425d6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions commons/c2cgeoportal_commons/models/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import colander
from deform.widget import HiddenWidget, SelectWidget, TextAreaWidget
from c2cgeoform.ext import colander_ext, deform_ext
import c2cwsgiutils.broadcast

from c2cgeoportal_commons.config import config
from c2cgeoportal_commons.models import Base, _
Expand All @@ -62,6 +63,7 @@ class InvalidateCacheEvent:
pass


@c2cwsgiutils.broadcast.decorator()
def cache_invalidate_cb(*args: List[Any]) -> None:
zope.event.notify(InvalidateCacheEvent())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ vars:
PRINT_URL: http://print:8080/print/
REDIS_HOST: redis
REDIS_PORT: 6379
C2C_REDIS_URL: redis://redis:6379
C2C_BROADCAST_PREFIX: c2cgeoportal_
tilecloudchain:
environment:
VISIBLE_ENTRY_POINT: '{docker_entry_point}'
Expand Down
20 changes: 19 additions & 1 deletion geoportal/c2cgeoportal_geoportal/views/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
from sqlalchemy.orm import subqueryload
from sqlalchemy.orm.exc import NoResultFound
from owslib.wms import WebMapService
import zope.event.classhandler

from c2cgeoportal_commons import models
from c2cgeoportal_commons.models import main, static
Expand Down Expand Up @@ -115,6 +116,7 @@ class Entry:
WFS_NS = "http://www.opengis.net/wfs"
default_ogc_server = None
external_ogc_server = None
server_wms_capabilities = {}

def __init__(self, request):
self.request = request
Expand Down Expand Up @@ -160,6 +162,14 @@ def __init__(self, request):
", ".join([i[0] for i in models.DBSession.query(main.OGCServer.name).all()])
)

from c2cgeoportal_commons.models.main import InvalidateCacheEvent

@zope.event.classhandler.handler(InvalidateCacheEvent)
def handle(event: InvalidateCacheEvent):
del event
Entry.server_wms_capabilities = {}


@view_config(route_name="testi18n", renderer="testi18n.html")
def testi18n(self): # pragma: no cover
_ = self.request.translate
Expand Down Expand Up @@ -200,6 +210,9 @@ def _get_metadatas(self, item, errors):
def _wms_getcap(self, ogc_server=None):
ogc_server = (ogc_server or self.default_ogc_server)

if ogc_server.id in self.server_wms_capabilities:
return self.server_wms_capabilities[ogc_server.id]

url, content, errors = self._wms_getcap_cached(
ogc_server, self._get_capabilities_cache_role_key(ogc_server)
)
Expand All @@ -215,6 +228,9 @@ def _wms_getcap(self, ogc_server=None):
"\nURL: {}\n{}".format(url, content)
errors.add(error)
log.error(error, exc_info=True)

self.server_wms_capabilities[ogc_server.id] = (wms, errors)

return wms, errors

@cache_region.cache_on_arguments()
Expand Down Expand Up @@ -1008,7 +1024,9 @@ def _get_functionalities(theme):
@staticmethod
@view_config(route_name="invalidate", renderer="json")
def invalidate_cache(): # pragma: no cover
invalidate_region()
import c2cgeoportal_commons.models.main

c2cgeoportal_commons.models.main.cache_invalidate_cb()
return {
"success": True
}
Expand Down

0 comments on commit 9c425d6

Please sign in to comment.