-
-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5332 from 3liz/backport-5262-to-release_3_9
[Backport release_3_9] PHP: caching QGIS Server metadata
- Loading branch information
Showing
8 changed files
with
271 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
class lizmapModuleUpgrader_requestscache extends jInstallerModule | ||
{ | ||
public $targetVersions = array( | ||
'3.9.0-pre', | ||
); | ||
public $date = '2025-01-29'; | ||
|
||
public function install() | ||
{ | ||
if ($this->firstExec('cacherequests')) { | ||
$profiles = new \Jelix\IniFile\IniModifier(jApp::varConfigPath('profiles.ini.php')); | ||
if (!$profiles->isSection('jcache:requests')) { | ||
$profiles->setValues(array( | ||
'enabled' => 1, | ||
'driver' => 'file', | ||
'ttl' => 0, | ||
), 'jcache:requests'); | ||
$profiles->save(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
lizmap/modules/lizmap/lib/Request/QgisServerMetadataRequestMatcher.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
/** | ||
* QGIS Server metadata request matcher to cache response. | ||
* | ||
* @author 3liz | ||
* @copyright 2025 3liz | ||
* | ||
* @see http://3liz.com | ||
* | ||
* @license Mozilla Public License : http://www.mozilla.org/MPL/ | ||
*/ | ||
|
||
namespace Lizmap\Request; | ||
|
||
use Kevinrob\GuzzleCache\Strategy\Delegate\RequestMatcherInterface; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
class QgisServerMetadataRequestMatcher implements RequestMatcherInterface | ||
{ | ||
/** | ||
* The QGIS Server host. | ||
* | ||
* @var string | ||
*/ | ||
protected $qgisServerHost; | ||
|
||
/** | ||
* The QGIS Server Metadata path. | ||
* | ||
* @var string | ||
*/ | ||
protected $qgisServerMetadataPath; | ||
|
||
/** | ||
* @param string $qgisServerMetadataUrl The QGIS Server metadata URL - It could be provided by Lizmap Services | ||
*/ | ||
public function __construct(string $qgisServerMetadataUrl) | ||
{ | ||
$urlInfo = parse_url($qgisServerMetadataUrl); | ||
$this->qgisServerHost = $urlInfo['host'] ?? 'localhost'; | ||
$this->qgisServerMetadataPath = $urlInfo['path'] ?? ''; | ||
} | ||
|
||
/** | ||
* @param RequestInterface $request | ||
* | ||
* @return bool | ||
*/ | ||
public function matches($request) | ||
{ | ||
return strpos($request->getUri()->getHost(), $this->qgisServerHost) !== false | ||
&& strpos($request->getUri()->getPath(), $this->qgisServerMetadataPath) !== false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
/** | ||
* Request cache storage to use in GuzzleCache with jCache. | ||
* | ||
* @author 3liz | ||
* @copyright 2025 3liz | ||
* | ||
* @see http://3liz.com | ||
* | ||
* @license Mozilla Public License : http://www.mozilla.org/MPL/ | ||
*/ | ||
|
||
namespace Lizmap\Request; | ||
|
||
use Kevinrob\GuzzleCache\CacheEntry; | ||
use Kevinrob\GuzzleCache\Storage\CacheStorageInterface; | ||
|
||
class RequestCacheStorage implements CacheStorageInterface | ||
{ | ||
/** | ||
* @var string jCache profile | ||
*/ | ||
protected $profile; | ||
|
||
/** | ||
* @param string $profile the jCache profile | ||
*/ | ||
public function __construct(string $profile) | ||
{ | ||
$this->profile = $profile; | ||
} | ||
|
||
/** | ||
* @param string $key | ||
* | ||
* @return null|CacheEntry the data or false | ||
*/ | ||
public function fetch($key) | ||
{ | ||
try { | ||
$cache = unserialize(\jCache::get($key, $this->profile)); | ||
if ($cache instanceof CacheEntry) { | ||
return $cache; | ||
} | ||
} catch (\Exception $ignored) { | ||
\jLog::logEx($ignored, 'error'); | ||
|
||
return null; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @param string $key | ||
* @param CacheEntry $data | ||
* | ||
* @return bool | ||
*/ | ||
public function save($key, $data) | ||
{ | ||
try { | ||
$lifeTime = $data->getTTL(); | ||
if ($lifeTime === 0) { | ||
return \jCache::set( | ||
$key, | ||
serialize($data), | ||
null, | ||
$this->profile | ||
); | ||
} | ||
if ($lifeTime > 0) { | ||
return \jCache::set( | ||
$key, | ||
serialize($data), | ||
$lifeTime, | ||
$this->profile | ||
); | ||
} | ||
} catch (\Exception $ignored) { | ||
// No fail if we can't save it the storage | ||
\jLog::logEx($ignored, 'error'); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @param string $key | ||
* | ||
* @return bool | ||
*/ | ||
public function delete($key) | ||
{ | ||
return \jCache::delete($key, $this->profile); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
tests/units/classes/Request/QgisServerMetadataRequestMatcherTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use GuzzleHttp\Psr7\Request; | ||
use Lizmap\Request\QgisServerMetadataRequestMatcher; | ||
|
||
class QgisServerMetadataRequestMatcherTest extends TestCase | ||
{ | ||
public function testMapHost() | ||
{ | ||
$matcher = new QgisServerMetadataRequestMatcher('http://map:8080/lizmap/server.json'); | ||
$request = new Request( | ||
'GET', | ||
'http://map:8080/lizmap/server.json', | ||
); | ||
$this->assertTrue($matcher->matches($request)); | ||
|
||
$request = new Request( | ||
'GET', | ||
'http://map:8080/ows/?SERVICE=WMS&REQUEST=Getcapabilities', | ||
); | ||
$this->assertFalse($matcher->matches($request)); | ||
|
||
$request = new Request( | ||
'GET', | ||
'http://localhost/qgis_mapserv.fcgi/lizmap/server.json', | ||
); | ||
$this->assertFalse($matcher->matches($request)); | ||
} | ||
|
||
public function testLocalHost() | ||
{ | ||
$matcher = new QgisServerMetadataRequestMatcher('http://localhost/qgis_mapserv.fcgi/lizmap/server.json'); | ||
$request = new Request( | ||
'GET', | ||
'http://localhost/qgis_mapserv.fcgi/lizmap/server.json', | ||
); | ||
$this->assertTrue($matcher->matches($request)); | ||
|
||
$request = new Request( | ||
'GET', | ||
'http://map:8080/lizmap/server.json', | ||
); | ||
$this->assertFalse($matcher->matches($request)); | ||
|
||
$request = new Request( | ||
'GET', | ||
'http://localhost/qgis_mapserv.fcgi?SERVICE=WMS&REQUEST=Getcapabilities', | ||
); | ||
$this->assertFalse($matcher->matches($request)); | ||
} | ||
} |