Yurik has uploaded a new change for review.

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

Change subject: Added caching and removed obsolete jsontext param
......................................................................

Added caching and removed obsolete jsontext param

* removed obsolete jsontext param in JCSingleton::getContent
* Added caching for jsonconfig content

Change-Id: I2f99f167610a706d3580e3577e5d1b006c5bd43e
---
M includes/JCSingleton.php
1 file changed, 37 insertions(+), 16 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/JsonConfig 
refs/changes/10/291010/1

diff --git a/includes/JCSingleton.php b/includes/JCSingleton.php
index f22dc6a..34ed06a 100644
--- a/includes/JCSingleton.php
+++ b/includes/JCSingleton.php
@@ -3,6 +3,7 @@
 
 use ContentHandler;
 use Exception;
+use MapCacheLRU;
 use stdClass;
 use TitleValue;
 use Title;
@@ -31,6 +32,11 @@
         * If false, presumes the namespace has been registered by core or 
another extension
         */
        static $namespaces = array();
+
+       /**
+        * @var MapCacheLRU[] contains a cache of recently requested content 
objects as namespace => cache
+        */
+       static $mapCacheLru = array();
 
        /**
         * Initializes singleton state by parsing $wgJsonConfig* values
@@ -274,34 +280,49 @@
        }
 
        /**
+        * Get content object from the local LRU cache, or null if doesn't exist
+        * @param TitleValue $titleValue
+        * @return null|JCContent
+        */
+       public static function getContentFromLocalCache( TitleValue $titleValue 
) {
+               if ( !array_key_exists( $titleValue->getNamespace(), 
self::$mapCacheLru ) ) {
+                       // TBD: should cache size be a config value?
+                       self::$mapCacheLru[$titleValue->getNamespace()] = 
$cache = new MapCacheLRU( 10 );
+               } else {
+                       $cache = 
self::$mapCacheLru[$titleValue->getNamespace()];
+               }
+
+               return $cache->get( $titleValue->getDBkey() );
+       }
+
+       /**
         * Get content object for the given title.
         * Namespace ID does not need to be defined in the current wiki,
         * as long as it is defined in $wgJsonConfigs.
         * @param TitleValue $titleValue
-        * @param string $jsonText obsolete, use parseContent() instead. If 
given, parses this text instead of what's stored in the database/cache
         * @return bool|JCContent Returns false if the title is not handled by 
the settings
         */
-       public static function getContent( TitleValue $titleValue, $jsonText = 
null ) {
+       public static function getContent( TitleValue $titleValue ) {
 
-               // TODO: remove $jsonText -- parseContent() should be used 
instead
+               $content = self::getContentFromLocalCache( $titleValue );
 
-               $conf = self::getMetadata( $titleValue );
-               if ( $conf ) {
-                       if ( is_string( $jsonText ) ) {
-                               $content = $jsonText;
-                       } else {
+               if ( $content === null ) {
+                       $conf = self::getMetadata( $titleValue );
+                       if ( $conf ) {
                                $store = new JCCache( $titleValue, $conf );
                                $content = $store->get();
+                               if ( is_string( $content ) ) {
+                                       // Convert string to the content object 
if needed
+                                       $handler = new JCContentHandler( 
$conf->model );
+                                       $content = 
$handler->unserializeContent( $content, null, false );
+                               }
+                       } else {
+                               $content = false;
                        }
-                       if ( is_string( $content ) ) {
-                               // Convert string to the content object if 
needed
-                               $handler = new JCContentHandler( $conf->model );
-                               return $handler->unserializeContent( $content, 
null, false );
-                       } elseif ( $content !== false ) {
-                               return $content;
-                       }
+                       self::$mapCacheLru[$titleValue->getNamespace()]->set( 
$titleValue->getDBkey(), $content );
                }
-               return false;
+
+               return $content;
        }
 
        /**

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2f99f167610a706d3580e3577e5d1b006c5bd43e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/JsonConfig
Gerrit-Branch: master
Gerrit-Owner: Yurik <[email protected]>

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

Reply via email to