Added getBatch() function to BagOStuff and optimized it for memcached.
authorAaron Schulz <aschulz@wikimedia.org>
Sat, 21 Apr 2012 18:07:18 +0000 (11:07 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Sat, 21 Apr 2012 18:07:18 +0000 (11:07 -0700)
Change-Id: Id8a3ed1ac7b22db7f7bf58ffde4be3285e8e2df9

includes/objectcache/BagOStuff.php
includes/objectcache/MemcachedPhpBagOStuff.php

index a4545ef..71469ab 100644 (file)
@@ -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
index 021dfb7..d2c7a2d 100644 (file)
@@ -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