From 73cea543365d27fe8496dd001ae46bcd3a406be1 Mon Sep 17 00:00:00 2001 From: Brian Evans Date: Thu, 29 Sep 2022 12:28:10 -0400 Subject: GentooPackages: Make use of the recommended WAN Cache This allows a more seemless callback as well Signed-off-by: Brian Evans --- GentooPackages/GentooPackages.php | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/GentooPackages/GentooPackages.php b/GentooPackages/GentooPackages.php index 2e3ff3e1..950d5d67 100644 --- a/GentooPackages/GentooPackages.php +++ b/GentooPackages/GentooPackages.php @@ -9,29 +9,21 @@ class GentooPackages { // implements MediaWiki\Hook\ParserFirstCallInitHook { if ($atom === NULL) { return "Package name missing"; } - $cacheExpiry = 60 * 60 * 24; // 1 day - $cacheKey = ['packageInfo', $atom, $type]; - $cacheKeyString = ObjectCache::getLocalClusterInstance()->makeKey(...array_values( $cacheKey )); try { - $packageInfo = ''; - $objCache = ObjectCache::getInstance( CACHE_ANYTHING ); - $cacheChunks = $objCache->get($cacheKeyString); - if ( !empty($cacheChunks) && is_array($cacheChunks) ) { - $packageInfo = array_shift( $cacheChunks ); - } - if ( empty( $packageInfo ) ) { - $packageInfo = static::fetchOrError($atom, $type); - $cacheChunks = [ $packageInfo ]; - $objCache->set($cacheKeyString, $cacheChunks, $cacheExpiry); - } - + $objCache = MediaWikiServices::getInstance()->getMainWANObjectCache(); + $cacheKeyString = $objCache->makeKey('packageInfo', $atom, $type); + $packageInfo = $objCache->getWithSetCallback($cacheKeyString, $objCache::TTL_DAY, + function( $oldValue, &$ttl, array &$setOpts ) use ( $atom, $type ) { + return self::fetchOrError($atom, $type); + } + ); return [$packageInfo, 'markerType' => 'nowiki']; } catch (Exception $ex) { return [$ex->getMessage(), 'markerType' => 'nowiki']; } } - private static function fetchOrError($atom, $type) { + public static function fetchOrError($atom, $type) { global $wgVersion; $url = "https://packages.gentoo.org/packages/${atom}.json"; if ($type !== 'use') { -- cgit v1.2.3-65-gdbad