diff options
author | Brian Evans <grknight@gentoo.org> | 2020-12-03 21:58:10 -0500 |
---|---|---|
committer | Brian Evans <grknight@gentoo.org> | 2020-12-03 21:58:10 -0500 |
commit | 80937db915c7277bbe3415686da6dcc990960fe6 (patch) | |
tree | 78d8e1032fd1219430b72783af4c3f37dd4c58f0 | |
parent | GentooToolbox: Implement new parser interface (diff) | |
download | extensions-80937db915c7277bbe3415686da6dcc990960fe6.tar.gz extensions-80937db915c7277bbe3415686da6dcc990960fe6.tar.bz2 extensions-80937db915c7277bbe3415686da6dcc990960fe6.zip |
GentooPackages: Attempt to cache the result of USE data
Signed-off-by: Brian Evans <grknight@gentoo.org>
-rw-r--r-- | GentooPackages/GentooPackages.php | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/GentooPackages/GentooPackages.php b/GentooPackages/GentooPackages.php index f8ecfd79..13ab7241 100644 --- a/GentooPackages/GentooPackages.php +++ b/GentooPackages/GentooPackages.php @@ -8,13 +8,26 @@ class GentooPackages { if ($atom === NULL) { return "Package name missing"; } else { - return [self::fetchOrError($atom, $type), 'markerType' => 'nowiki']; + $cache = new CacheHelper(); + $cache->setExpiry(60 * 60 * 24); // 1 day + $cache->setCacheKey(['packageInfo', $atom, $type]); + try { + $packageInfo = $cache->getCachedValue('self::fetchOrError', [$atom, $type]); + $cache->saveCache(); + return [$packageInfo, 'markerType' => 'nowiki']; + } catch (Exception $ex) { + return [$ex->message, 'markerType' => 'nowiki']; + } } } static function fetchOrError($atom, $type) { global $wgVersion; $url = "https://packages.gentoo.org/packages/${atom}.json"; + if ($type !== 'use') { + throw new UnexpectedValueException('<div class="alert alert-danger">Unknown type parameter value.</div>'); + } + if(version_compare( $wgVersion, '1.33', '<=' )) $json_str = Http::get($url); else { @@ -28,15 +41,10 @@ class GentooPackages { } if ($json_str === false) { - return '<div class="alert alert-danger">Cannot load package information. Is the atom <em>' . htmlspecialchars($atom) . '</em> correct?</div>'; + throw new ErrorException('<div class="alert alert-danger">Cannot load package information. Is the atom <em>' . htmlspecialchars($atom) . '</em> correct?</div>'); } else { $json = json_decode($json_str, true); - - if ($type === 'use') { - return self::render($json); - } else { - return '<div class="alert alert-danger">Unknown type parameter value.</div>'; - } + return self::render($json); } } |