Skip to content

Commit

Permalink
Merge pull request #95 from rldhont/fix-cache-getcapabilities-cadastre
Browse files Browse the repository at this point in the history
[Fix] Utilisation du cache handler de projet
  • Loading branch information
rldhont authored Oct 12, 2023
2 parents 5a0b7c6 + 4dea38a commit a714cf7
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 a714cf7

Please sign in to comment.