From d4895d5c706bb0e69302345f5e52813ba65cdb82 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Sat, 17 Oct 2015 15:12:20 -0700 Subject: [PATCH] Add make(Global)Key() method to WANObjectCache Change-Id: I8e739fbe8614c9bd1c3595bbf16c8cd423aff7cc --- includes/libs/objectcache/BagOStuff.php | 4 +- includes/libs/objectcache/WANObjectCache.php | 40 +++++++++++++++----- includes/objectcache/ObjectCache.php | 4 ++ 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/includes/libs/objectcache/BagOStuff.php b/includes/libs/objectcache/BagOStuff.php index fc74985ed3..95c1bc76c7 100644 --- a/includes/libs/objectcache/BagOStuff.php +++ b/includes/libs/objectcache/BagOStuff.php @@ -628,7 +628,7 @@ abstract class BagOStuff implements LoggerAwareInterface { * Make a global cache key. * * @since 1.27 - * @param string $args,... + * @param string ... Key component (variadic) * @return string */ public function makeGlobalKey() { @@ -639,7 +639,7 @@ abstract class BagOStuff implements LoggerAwareInterface { * Make a cache key, scoped to this instance's keyspace. * * @since 1.27 - * @param string $args,... + * @param string ... Key component (variadic) * @return string */ public function makeKey() { diff --git a/includes/libs/objectcache/WANObjectCache.php b/includes/libs/objectcache/WANObjectCache.php index 3b9c832be7..1608d4532e 100644 --- a/includes/libs/objectcache/WANObjectCache.php +++ b/includes/libs/objectcache/WANObjectCache.php @@ -283,7 +283,7 @@ class WANObjectCache { * $setOpts = Database::getCacheSetOptions( $dbr ); * // Fetch the row from the DB * $row = $dbr->selectRow( ... ); - * $key = wfMemcKey( 'building', $buildingId ); + * $key = $cache->makeKey( 'building', $buildingId ); * $cache->set( $key, $row, 86400, $setOpts ); * @endcode * @@ -372,7 +372,7 @@ class WANObjectCache { * ... ... * // Update the row in the DB * $dbw->update( ... ); - * $key = wfMemcKey( 'homes', $homeId ); + * $key = $cache->makeKey( 'homes', $homeId ); * // Purge the corresponding cache entry just before committing * $dbw->onTransactionPreCommitOrIdle( function() use ( $cache, $key ) { * $cache->delete( $key ); @@ -536,7 +536,7 @@ class WANObjectCache { * @code * $catInfo = $cache->getWithSetCallback( * // Key to store the cached value under - * wfMemcKey( 'cat-attributes', $catId ), + * $cache->makeKey( 'cat-attributes', $catId ), * // Time-to-live (seconds) * 60, * // Function that derives the new key value @@ -554,7 +554,7 @@ class WANObjectCache { * @code * $catConfig = $cache->getWithSetCallback( * // Key to store the cached value under - * wfMemcKey( 'site-cat-config' ), + * $cache->makeKey( 'site-cat-config' ), * // Time-to-live (seconds) * 86400, * // Function that derives the new key value @@ -567,7 +567,7 @@ class WANObjectCache { * }, * array( * // Calling touchCheckKey() on this key invalidates the cache - * 'checkKeys' => array( wfMemcKey( 'site-cat-config' ) ), + * 'checkKeys' => array( $cache->makeKey( 'site-cat-config' ) ), * // Try to only let one datacenter thread manage cache updates at a time * 'lockTSE' => 30 * ) @@ -578,7 +578,7 @@ class WANObjectCache { * @code * $catState = $cache->getWithSetCallback( * // Key to store the cached value under - * wfMemcKey( 'cat-state', $cat->getId() ), + * $cache->makeKey( 'cat-state', $cat->getId() ), * // Time-to-live (seconds) * 900, * // Function that derives the new key value @@ -594,9 +594,9 @@ class WANObjectCache { * // The "check" keys that represent things the value depends on; * // Calling touchCheckKey() on any of them invalidates the cache * 'checkKeys' => array( - * wfMemcKey( 'sustenance-bowls', $cat->getRoomId() ), - * wfMemcKey( 'people-present', $cat->getHouseId() ), - * wfMemcKey( 'cat-laws', $cat->getCityId() ), + * $cache->makeKey( 'sustenance-bowls', $cat->getRoomId() ), + * $cache->makeKey( 'people-present', $cat->getHouseId() ), + * $cache->makeKey( 'cat-laws', $cat->getCityId() ), * ) * ) * ); @@ -606,7 +606,7 @@ class WANObjectCache { * @code * $lastCatActions = $cache->getWithSetCallback( * // Key to store the cached value under - * wfMemcKey( 'cat-last-actions', 100 ), + * $cache->makeKey( 'cat-last-actions', 100 ), * // Time-to-live (seconds) * 10, * // Function that derives the new key value @@ -778,6 +778,26 @@ class WANObjectCache { return $value; } + /** + * @see BagOStuff::makeKey() + * @param string ... Key component + * @return string + * @since 1.27 + */ + public function makeKey() { + return call_user_func_array( array( $this->cache, __FUNCTION__ ), func_get_args() ); + } + + /** + * @see BagOStuff::makeGlobalKey() + * @param string ... Key component + * @return string + * @since 1.27 + */ + public function makeGlobalKey() { + return call_user_func_array( array( $this->cache, __FUNCTION__ ), func_get_args() ); + } + /** * Get the "last error" registered; clearLastError() should be called manually * @return int ERR_* constant for the "last error" registry diff --git a/includes/objectcache/ObjectCache.php b/includes/objectcache/ObjectCache.php index d739d8e300..098247a6cc 100644 --- a/includes/objectcache/ObjectCache.php +++ b/includes/objectcache/ObjectCache.php @@ -68,6 +68,10 @@ use MediaWiki\Logger\LoggerFactory; * - wfGetCache( $cacheType ) * Get a specific cache type by key in $wgObjectCaches. * + * All the above cache instances (BagOStuff and WANObjectCache) have their makeKey() + * method scoped to the *current* wiki ID. Use makeGlobalKey() to avoid this scoping + * when using keys that need to be shared amongst wikis. + * * @ingroup Cache */ class ObjectCache { -- 2.20.1