Yurik has uploaded a new change for review.

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

Change subject: Cache JSON objects in memcached
......................................................................

Cache JSON objects in memcached

* The graph data should now be available immediatelly
  after parsing, without waiting for SQL replication
* Graph URL will now continue working for some time
  after being removed from the page. This is not
  a complete solution, and we will need to implement
  something more permanent for the older graphs.

Change-Id: I0c6bc4bb4aa21d4b1647e04601806da345a008ef
---
M includes/ApiGraph.php
M includes/Graph.body.php
2 files changed, 49 insertions(+), 20 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Graph 
refs/changes/60/263160/1

diff --git a/includes/ApiGraph.php b/includes/ApiGraph.php
index 09b69a0..3adef4c 100644
--- a/includes/ApiGraph.php
+++ b/includes/ApiGraph.php
@@ -88,28 +88,31 @@
         * @return string
         */
        private function getFromStorage( $title, $hash ) {
-               $title = Title::newFromText( $title );
-               if ( !$title || !$title->exists() || !$title->userCan( 'read', 
$this->getUser() ) ) {
-                       $this->dieUsage( "Invalid title given.", "invalidtitle" 
);
-               }
+               $graph = Singleton::getDataByHash( $hash );
 
-               $ppValue = $this->getDB()->selectField( 'page_props', 
'pp_value', array(
-                       'pp_page' => $title->getArticleID(),
-                       'pp_propname' => 'graph_specs',
-               ), __METHOD__ );
-
-               $graph = false;
-               if ( $ppValue ) {
-                       // Copied from TemplateDataBlob.php:newFromDatabase()
-                       // Handle GZIP compression. \037\213 is the header for 
GZIP files.
-                       if ( substr( $ppValue, 0, 2 ) === "\037\213" ) {
-                               $ppValue = gzdecode( $ppValue );
+               if ( !$graph ) {
+                       $title = Title::newFromText( $title );
+                       if ( !$title || !$title->exists() || !$title->userCan( 
'read', $this->getUser() ) ) {
+                               $this->dieUsage( "Invalid title given.", 
"invalidtitle" );
                        }
-                       $st = FormatJson::parse( $ppValue );
-                       if ( $st->isOK() ) {
-                               $allGraphs = $st->getValue();
-                               if ( property_exists( $allGraphs, $hash ) ) {
-                                       $graph = $allGraphs->$hash;
+
+                       $ppValue = $this->getDB()->selectField( 'page_props', 
'pp_value', array(
+                               'pp_page' => $title->getArticleID(),
+                               'pp_propname' => 'graph_specs',
+                       ), __METHOD__ );
+
+                       if ( $ppValue ) {
+                               // Copied from 
TemplateDataBlob.php:newFromDatabase()
+                               // Handle GZIP compression. \037\213 is the 
header for GZIP files.
+                               if ( substr( $ppValue, 0, 2 ) === "\037\213" ) {
+                                       $ppValue = gzdecode( $ppValue );
+                               }
+                               $st = FormatJson::parse( $ppValue );
+                               if ( $st->isOK() ) {
+                                       $allGraphs = $st->getValue();
+                                       if ( property_exists( $allGraphs, $hash 
) ) {
+                                               $graph = $allGraphs->$hash;
+                                       }
                                }
                        }
                }
diff --git a/includes/Graph.body.php b/includes/Graph.body.php
index 0c06206..e587138 100644
--- a/includes/Graph.body.php
+++ b/includes/Graph.body.php
@@ -189,6 +189,8 @@
                $specs[$hash] = $data;
                $parserOutput->setExtensionData( 'graph_specs', $specs );
 
+               self::saveDataToCache( $hash, $data );
+
                if ( $isPreview || !$wgGraphImgServiceUrl ) {
                        // Always do client-side rendering
                        $attribs = self::buildDivAttributes( 'always', $data, 
$hash );
@@ -197,6 +199,7 @@
                        $parserOutput->setExtensionData( 'graph_live_specs', 
$liveSpecs );
                        $html = ''; // will be injected with a <canvas> tag
                } else {
+
                        // Image from Graphoid
                        $server = rawurlencode( $wgServerName );
                        $title = !$title ? '' : rawurlencode( 
$title->getPrefixedDBkey() );
@@ -237,6 +240,28 @@
 
                return Html::rawElement( 'div', $attribs, $html );
        }
+
+       /**
+        * Store graph data in the memcached
+        * @param $hash string
+        * @param $data mixed
+        */
+       private static function saveDataToCache( $hash, $data ) {
+               /** @var $wgMemc \BagOStuff */
+               global $wgMemc;
+               $wgMemc->add( $wgMemc->makeKey( 'graph-data', $hash ), $data );
+       }
+
+       /**
+        * Gets data for a key, or false on failure
+        * @param $hash string
+        * @return mixed
+        */
+       public static function getDataByHash( $hash ) {
+               /** @var $wgMemc \BagOStuff */
+               global $wgMemc;
+               return $wgMemc->get( $wgMemc->makeKey( 'graph-data', $hash ) );
+       }
 }
 
 /**
@@ -260,6 +285,7 @@
 
        protected function fillParserOutput( Title $title, $revId, 
ParserOptions $options, $generateHtml,
                                             ParserOutput &$output ) {
+               /** @var $wgParser Parser */
                global $wgParser;
                $text = $this->getNativeData();
                $parser = $wgParser->getFreshParser();

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0c6bc4bb4aa21d4b1647e04601806da345a008ef
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Graph
Gerrit-Branch: master
Gerrit-Owner: Yurik <yu...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to