Ori.livneh has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/250298

Change subject: Add `MapCache` class, sans LRU
......................................................................

Add `MapCache` class, sans LRU

Make MapCacheLRU extend an even simpler base class which provides the same
get/set/clear interface for an associative array but which does not implement
LRU eviction or a size limit.

Change-Id: If9d95956e21194e3925604b1d4bf14a5f2f81b5d
---
M autoload.php
M includes/libs/MapCacheLRU.php
2 files changed, 85 insertions(+), 43 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/98/250298/1

diff --git a/autoload.php b/autoload.php
index 65a84bf..5e93c96 100644
--- a/autoload.php
+++ b/autoload.php
@@ -736,6 +736,7 @@
        'MaintenanceFormatInstallDoc' => __DIR__ . 
'/maintenance/formatInstallDoc.php',
        'MalformedTitleException' => __DIR__ . 
'/includes/title/MalformedTitleException.php',
        'ManualLogEntry' => __DIR__ . '/includes/logging/LogEntry.php',
+       'MapCache' => __DIR__ . '/includes/libs/MapCacheLRU.php',
        'MapCacheLRU' => __DIR__ . '/includes/libs/MapCacheLRU.php',
        'MappedDiff' => __DIR__ . '/includes/diff/DairikiDiff.php',
        'MappedIterator' => __DIR__ . '/includes/libs/MappedIterator.php',
diff --git a/includes/libs/MapCacheLRU.php b/includes/libs/MapCacheLRU.php
index a49eb01..aebcb8f 100644
--- a/includes/libs/MapCacheLRU.php
+++ b/includes/libs/MapCacheLRU.php
@@ -22,6 +22,85 @@
  */
 use Wikimedia\Assert\Assert;
 
+
+/**
+ * Trivial get/set interface for an associative array
+ *
+ * @ingroup Cache
+ * @since 1.27
+ */
+class MapCache {
+       /** @var array */
+       protected $cache = array(); // (key => value)
+
+       /**
+        * @param array $initialData Map of key/values to pre-load.
+        */
+       public function __construct( $initialData = array() ) {
+               $this->cache = $initialData;
+       }
+
+       /**
+        * Set a key/value pair.
+        *
+        * @param string $key
+        * @param mixed $value
+        * @return void
+        */
+       public function set( $key, $value ) {
+               $this->cache[$key] = $value;
+       }
+
+       /**
+        * Check if a key exists
+        *
+        * @param string $key
+        * @return bool
+        */
+       public function has( $key ) {
+               return array_key_exists( $key, $this->cache );
+       }
+
+       /**
+        * Get the value for a key.
+        * This returns null if the key is not set.
+        *
+        * @param string $key
+        * @return mixed
+        */
+       public function get( $key ) {
+               if ( !array_key_exists( $key, $this->cache ) ) {
+                       return null;
+               }
+               return $this->cache[$key];
+       }
+
+       /**
+        * @return array
+        * @since 1.25
+        */
+       public function getAllKeys() {
+               return array_keys( $this->cache );
+       }
+
+       /**
+        * Clear one or several cache entries, or all cache entries
+        *
+        * @param string|array $keys
+        * @return void
+        */
+       public function clear( $keys = null ) {
+               if ( $keys === null ) {
+                       $this->cache = array();
+               } else {
+                       foreach ( (array)$keys as $key ) {
+                               unset( $this->cache[$key] );
+                       }
+               }
+       }
+}
+
+
 /**
  * Handles a simple LRU key/value map with a maximum number of entries
  *
@@ -31,10 +110,7 @@
  * @ingroup Cache
  * @since 1.23
  */
-class MapCacheLRU {
-       /** @var array */
-       protected $cache = array(); // (key => value)
-
+class MapCacheLRU extends MapCache {
        protected $maxCacheKeys; // integer; max entries
 
        /**
@@ -65,17 +141,7 @@
                        $evictKey = key( $this->cache );
                        unset( $this->cache[$evictKey] );
                }
-               $this->cache[$key] = $value;
-       }
-
-       /**
-        * Check if a key exists
-        *
-        * @param string $key
-        * @return bool
-        */
-       public function has( $key ) {
-               return array_key_exists( $key, $this->cache );
+               parent::set( $key, value );
        }
 
        /**
@@ -87,35 +153,10 @@
         * @return mixed
         */
        public function get( $key ) {
-               if ( !array_key_exists( $key, $this->cache ) ) {
-                       return null;
+               if ( $this->has( $key ) ) {
+                       $this->ping( $key );
                }
-               $this->ping( $key );
-               return $this->cache[$key];
-       }
-
-       /**
-        * @return array
-        * @since 1.25
-        */
-       public function getAllKeys() {
-               return array_keys( $this->cache );
-       }
-
-       /**
-        * Clear one or several cache entries, or all cache entries
-        *
-        * @param string|array $keys
-        * @return void
-        */
-       public function clear( $keys = null ) {
-               if ( $keys === null ) {
-                       $this->cache = array();
-               } else {
-                       foreach ( (array)$keys as $key ) {
-                               unset( $this->cache[$key] );
-                       }
-               }
+               return parent::get( $key );
        }
 
        /**

-- 
To view, visit https://gerrit.wikimedia.org/r/250298
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If9d95956e21194e3925604b1d4bf14a5f2f81b5d
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Ori.livneh <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to