From 80937db915c7277bbe3415686da6dcc990960fe6 Mon Sep 17 00:00:00 2001 From: Brian Evans Date: Thu, 3 Dec 2020 21:58:10 -0500 Subject: GentooPackages: Attempt to cache the result of USE data Signed-off-by: Brian Evans --- GentooPackages/GentooPackages.php | 24 ++++++++++++++++-------- 1 file 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('
Unknown type parameter value.
'); + } + if(version_compare( $wgVersion, '1.33', '<=' )) $json_str = Http::get($url); else { @@ -28,15 +41,10 @@ class GentooPackages { } if ($json_str === false) { - return '
Cannot load package information. Is the atom ' . htmlspecialchars($atom) . ' correct?
'; + throw new ErrorException('
Cannot load package information. Is the atom ' . htmlspecialchars($atom) . ' correct?
'); } else { $json = json_decode($json_str, true); - - if ($type === 'use') { - return self::render($json); - } else { - return '
Unknown type parameter value.
'; - } + return self::render($json); } } -- cgit v1.2.3-65-gdbad