Skip to content

Commit

Permalink
[Fix] Utilisation du cache handler de projet
Browse files Browse the repository at this point in the history
Afin d'optimiser le module cadastre, le getcapabilities cadastre doit être mis en cache avec le handler du projet.

Funded by [Conseil Département du Calvados](https://www.calvados.fr)
  • Loading branch information
rldhont committed Oct 6, 2023
1 parent 5a0b7c6 commit 4dea38a
Showing 1 changed file with 35 additions and 33 deletions.
68 changes: 35 additions & 33 deletions cadastre/classes/lizmapCadastreRequest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,29 @@ public function process_getcapabilities()

protected function getcapabilities()
{
$appContext = $this->appContext;
// Get cached session
$key = session_id() . '-' .
$this->project->getRepository()->getKey() . '-' .
$this->project->getKey() . '-' .
$this->param('service') . '-getcapabilities';
if (jAuth::isConnected()) {
$juser = jAuth::getUserSession();
// the cache should be unique between each user/service because the
// request content depends on rights of the user
$key = session_id() . '-' . $this->param('service');
$version = $this->param('version');
if ($version) {
$key .= '-' . $version;
}
if ($appContext->UserIsConnected()) {
$juser = $appContext->getUserSession();
$key .= '-' . $juser->login;
}
$key = sha1($key);
$key = 'getcapabilities-' . sha1($key);
$cached = false;

try {
$cached = jCache::get($key, 'qgisprojects');
$cached = $this->project->getCacheHandler()->getProjectRelatedDataCache($key);
} catch (Exception $e) {
// if qgisprojects profile does not exist, or if there is an
// other error about the cache, let's log it
jLog::logEx($e, 'error');
}
// invalid cache
if ($cached !== false && $cached['mtime'] < $this->project->getFileTime()) {
$cached = false;
}
// return cached data
if ($cached !== false) {
return (object) array(
Expand All @@ -53,38 +53,40 @@ protected function getcapabilities()
);
}

$querystring = $this->constructUrl();

// Get remote data
list($data, $mime, $code) = lizmapProxy::getRemoteData($querystring);
$response = $this->request();

// Retry if 500 error ( hackish, but QGIS Server segfault sometimes with cache issue )
if ($code == 500) {
// Get remote data
list($data, $mime, $code) = lizmapProxy::getRemoteData($querystring);
$response = $this->request();
}

if ($mime != 'text/json' && $mime != 'application/json') {
$code = 400;
$mime = 'application/json';
$data = json_encode((object) array(
'status' => 'fail',
'message' => 'Cadastre - Plugin non disponible',
));
if ($response->mime != 'text/json' && $response->mime != 'application/json') {
return (object) array(
'code' => 400,
'mime' => 'application/json',
'data' => json_encode((object) array(
'status' => 'fail',
'message' => 'Cadastre - Plugin non disponible',
)),
'cached' => false,
);
}

$cached = array(
'mtime' => $this->project->getFileTime(),
'code' => $code,
'mime' => $mime,
'data' => $data,
);
$cached = jCache::set($key, $cached, 3600, 'qgisprojects');
if ($response->code == 200) {
$cachedContent = array(
'code' => $response->code,
'mime' => $response->mime,
'data' => $response->data,
);
$cached = $this->project->getCacheHandler()->setProjectRelatedDataCache($key, $cachedContent, 3600);
}

return (object) array(
'code' => $code,
'mime' => $mime,
'data' => $data,
'code' => $response->code,
'mime' => $response->mime,
'data' => $response->data,
'cached' => $cached,
);
}
Expand Down

0 comments on commit 4dea38a

Please sign in to comment.