Skip to content
This repository was archived by the owner on Oct 24, 2023. It is now read-only.

Commit 9e9194f

Browse files
committed
Add experimental project settings cache, enabled via __QWC_CONFIG_SERVICE_PROJECT_SETTINGS_CACHE=1
1 parent a37565a commit 9e9194f

File tree

1 file changed

+49
-16
lines changed

1 file changed

+49
-16
lines changed

ogc_service_permission.py

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ def __init__(self, default_allow, config_models, logger):
3030
self.qgis_server_url = os.environ.get('QGIS_SERVER_URL',
3131
'http://localhost:8001/ows/').rstrip('/') + '/'
3232

33+
self.project_settings_cache = {}
34+
3335
def permissions(self, params, username, group, session):
3436
"""Query permissions for OGC service.
3537
@@ -75,6 +77,16 @@ def permissions(self, params, username, group, session):
7577

7678
return permissions
7779

80+
def themesConfigMTime(self):
81+
qwc2_path = os.environ.get('QWC2_PATH', 'qwc2/')
82+
themes_config_path = os.environ.get(
83+
'QWC2_THEMES_CONFIG', os.path.join(qwc2_path, 'themesConfig.json')
84+
)
85+
86+
if os.path.isfile(themes_config_path):
87+
return os.path.getmtime(themes_config_path)
88+
return -1
89+
7890
def parseProjectSettings(self, ows_name, ows_type):
7991
"""Get complete OGC service permissions from GetProjectSettings.
8092
@@ -85,22 +97,43 @@ def parseProjectSettings(self, ows_name, ows_type):
8597
"""
8698
permissions = {}
8799

88-
# get GetProjectSettings
89-
response = requests.get(
90-
urljoin(self.qgis_server_url, ows_name),
91-
params={
92-
'SERVICE': ows_type,
93-
'VERSION': '1.3.0',
94-
'REQUEST': 'GetProjectSettings'
95-
},
96-
timeout=30
97-
)
98-
99-
if response.status_code != requests.codes.ok:
100-
self.logger.warn(
101-
"Could not get GetProjectSettings: %s", response.content
100+
cache = os.environ.get("__QWC_CONFIG_SERVICE_PROJECT_SETTINGS_CACHE", "0") == "1"
101+
if cache and \
102+
ows_type in self.project_settings_cache and \
103+
ows_name in self.project_settings_cache[ows_type] and \
104+
self.project_settings_cache[ows_type][ows_name]["timestamp"] != -1 and \
105+
self.project_settings_cache[ows_type][ows_name]["timestamp"] >= self.themesConfigMTime():
106+
document = self.project_settings_cache[ows_type][ows_name]["document"]
107+
else:
108+
# get GetProjectSettings
109+
response = requests.get(
110+
urljoin(self.qgis_server_url, ows_name),
111+
params={
112+
'SERVICE': ows_type,
113+
'VERSION': '1.3.0',
114+
'REQUEST': 'GetProjectSettings'
115+
},
116+
timeout=30
102117
)
103-
return permissions
118+
119+
if response.status_code != requests.codes.ok:
120+
self.logger.warn(
121+
"Could not get GetProjectSettings: %s", response.content
122+
)
123+
return permissions
124+
125+
document = response.content
126+
127+
if cache:
128+
if not ows_type in self.project_settings_cache:
129+
self.project_settings_cache[ows_type] = {}
130+
if not ows_name in self.project_settings_cache[ows_type]:
131+
self.project_settings_cache[ows_type][ows_name] = {}
132+
self.project_settings_cache[ows_type][ows_name] = {
133+
"document": document,
134+
"timestamp": self.themesConfigMTime()
135+
}
136+
104137

105138
# parse GetProjectSettings XML
106139
ElementTree.register_namespace('', 'http://www.opengis.net/wms')
@@ -109,7 +142,7 @@ def parseProjectSettings(self, ows_name, ows_type):
109142
ElementTree.register_namespace(
110143
'xlink', 'http://www.w3.org/1999/xlink'
111144
)
112-
root = ElementTree.fromstring(response.content)
145+
root = ElementTree.fromstring(document)
113146

114147
# use default namespace for XML search
115148
# namespace dict

0 commit comments

Comments
 (0)