From 8019ebda16623ef8c9109003fb9713fdba871afa Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Sat, 21 Apr 2012 11:07:18 -0700 Subject: [PATCH] Added getBatch() function to BagOStuff and optimized it for memcached. Change-Id: Id8a3ed1ac7b22db7f7bf58ffde4be3285e8e2df9 --- includes/objectcache/BagOStuff.php | 15 +++++++++++++++ .../objectcache/MemcachedPhpBagOStuff.php | 19 ++++++++++++++----- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/includes/objectcache/BagOStuff.php b/includes/objectcache/BagOStuff.php index a4545ef83b..71469ab345 100644 --- a/includes/objectcache/BagOStuff.php +++ b/includes/objectcache/BagOStuff.php @@ -61,6 +61,21 @@ abstract class BagOStuff { */ abstract public function get( $key ); + /** + * Get an associative array containing the item for each of the given keys. + * Each item will be false if it does not exist. + * @param $keys Array List of strings + * + * @return Array + */ + public function getBatch( array $keys ) { + $res = array(); + foreach ( $keys as $key ) { + $res[$key] = $this->get( $key ); + } + return $res; + } + /** * Set an item. * @param $key string diff --git a/includes/objectcache/MemcachedPhpBagOStuff.php b/includes/objectcache/MemcachedPhpBagOStuff.php index 021dfb723e..d2c7a2dfc6 100644 --- a/includes/objectcache/MemcachedPhpBagOStuff.php +++ b/includes/objectcache/MemcachedPhpBagOStuff.php @@ -63,6 +63,15 @@ class MemcachedPhpBagOStuff extends BagOStuff { return $this->client->get( $this->encodeKey( $key ) ); } + /** + * @param $keys Array + * @return Array + */ + public function getBatch( array $keys ) { + $callback = array( $this, 'encodeKey' ); + return $this->client->get_multi( array_map( $callback, $keys ) ); + } + /** * @param $key string * @param $value @@ -137,7 +146,7 @@ class MemcachedPhpBagOStuff extends BagOStuff { } /** - * Get the underlying client object. This is provided for debugging + * Get the underlying client object. This is provided for debugging * purposes. * * @return MemCachedClientforWiki @@ -149,14 +158,14 @@ class MemcachedPhpBagOStuff extends BagOStuff { /** * Encode a key for use on the wire inside the memcached protocol. * - * We encode spaces and line breaks to avoid protocol errors. We encode - * the other control characters for compatibility with libmemcached + * We encode spaces and line breaks to avoid protocol errors. We encode + * the other control characters for compatibility with libmemcached * verify_key. We leave other punctuation alone, to maximise backwards * compatibility. * @return string */ public function encodeKey( $key ) { - return preg_replace_callback( '/[\x00-\x20\x25\x7f]+/', + return preg_replace_callback( '/[\x00-\x20\x25\x7f]+/', array( $this, 'encodeKeyCallback' ), $key ); } @@ -165,7 +174,7 @@ class MemcachedPhpBagOStuff extends BagOStuff { } /** - * Decode a key encoded with encodeKey(). This is provided as a convenience + * Decode a key encoded with encodeKey(). This is provided as a convenience * function for debugging. * * @param $key string -- 2.20.1