[MediaWiki-commits] [Gerrit] Cache JSON objects in memcached - change (mediawiki...Graph)

2016-01-11 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged.

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.

Bug: T122489
Bug: T119043
Change-Id: I0c6bc4bb4aa21d4b1647e04601806da345a008ef
---
M includes/ApiGraph.php
M includes/Graph.body.php
2 files changed, 54 insertions(+), 21 deletions(-)

Approvals:
  MaxSem: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/ApiGraph.php b/includes/ApiGraph.php
index 09b69a0..ad71a9a 100644
--- a/includes/ApiGraph.php
+++ b/includes/ApiGraph.php
@@ -88,28 +88,35 @@
 * @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" 
);
-   }
+   /** @var $wgMemc \BagOStuff */
+   global $wgMemc;
+   $graph = $wgMemc->get( Singleton::makeCacheKey( $hash ) );
+   // NOTE: Very strange wgMemc feature: Even though we store the 
data structure into wgMemc
+   // by JSON-encoding and gzip-ing it, when we get it out it is 
already in the original form.
 
-   $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 8e20329..f47901d 100644
--- a/includes/Graph.body.php
+++ b/includes/Graph.body.php
@@ -185,9 +185,12 @@
// Calculate hash and store graph definition in graph_specs 
extension data
$specs = $parserOutput->getExtensionData( 'graph_specs' ) ?: 
array();
// Make sure that multiple json blobs that only differ in 
spacing hash the same
-   $hash = sha1( FormatJson::encode( $data, false, 
FormatJson::ALL_OK ) );
+   $dataAsStr = FormatJson::encode( $data, false, 
FormatJson::ALL_OK );
+   $hash = sha1( $dataAsStr );
$specs[$hash] = $data;
$parserOutput->setExtensionData( 'graph_specs', $specs );
+
+   self::saveDataToCache( $hash, $dataAsStr );
 
if ( $isPreview || 

[MediaWiki-commits] [Gerrit] Cache JSON objects in memcached - change (mediawiki...Graph)

2016-01-08 Thread Yurik (Code Review)
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  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