tosfos has uploaded a new change for review.

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

Change subject: Switch cache to use internal ObjectCache
......................................................................

Switch cache to use internal ObjectCache

* deprecate GooglePlaces table

Change-Id: Ic51b5c223d5bc0779a47e3cc90b612d6c3688404
---
M GooglePlaces.hooks.php
D GooglePlaces.sql
M GooglePlacesCache.php
M extension.json
4 files changed, 27 insertions(+), 56 deletions(-)


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

diff --git a/GooglePlaces.hooks.php b/GooglePlaces.hooks.php
index 5749232..346e688 100644
--- a/GooglePlaces.hooks.php
+++ b/GooglePlaces.hooks.php
@@ -29,22 +29,13 @@
 
        public static function googlePlacesType( Parser &$parser, $placeID, 
$type, $field = '' ) {
                $details = self::getDetails( $placeID );
-               $output = self::getArrayElementFromType( 
$details['result']['address_components'], $type, $field );
+               $output = self::getArrayElementFromType(
+                               $details['result']['address_components'], 
$type, $field );
 
                self::insertPoweredBy();
                $output .= self::getTOSRequiredHTML( $details );
 
                return $output;
-       }
-
-       /**
-        *
-        * @param DatabaseUpdater $updater
-        * @return boolean
-        */
-       public static function onLoadExtensionSchemaUpdates( DatabaseUpdater 
$updater ) {
-               $updater->addExtensionTable( GooglePlacesCache::TABLE, __DIR__ 
. '/GooglePlaces.sql', true );
-               return true;
        }
 
        /**
@@ -58,8 +49,7 @@
        private static function getDetails( $placeID ) {
                global $wgGooglePlacesAPIKey, $wgGooglePlacesExpiry;
 
-               $request = array( 'api-key' => $wgGooglePlacesAPIKey, 
'place-id' => $placeID );
-               $details = GooglePlacesCache::getCache( $request );
+               $details = GooglePlacesCache::getCache( $wgGooglePlacesAPIKey, 
$placeID );
 
                if ( !$details ) {
                        $details = self::getDetailsFromGoogleAPI( 
$wgGooglePlacesAPIKey, $placeID );
@@ -68,7 +58,7 @@
                                return self::getAnyErrors( $details['errors'] );
                        }
 
-                       GooglePlacesCache::setCache( $request, $details, 
$wgGooglePlacesExpiry );
+                       GooglePlacesCache::setCache( $wgGooglePlacesAPIKey, 
$placeID, $details, $wgGooglePlacesExpiry );
                }
                return $details;
        }
@@ -104,7 +94,8 @@
                foreach ( $errors as $errorMessage ) {
                        $error .= ' ' . $errorMessage;
                }
-               return Html::element( 'strong', array( 'class' => array( 
'error', 'googleplaces-error' ) ), $error );
+               return Html::element(
+                               'strong', array( 'class' => array( 'error', 
'googleplaces-error' ) ), $error );
        }
 
        /**
diff --git a/GooglePlaces.sql b/GooglePlaces.sql
deleted file mode 100644
index 66a54b6..0000000
--- a/GooglePlaces.sql
+++ /dev/null
@@ -1,6 +0,0 @@
-CREATE TABLE IF NOT EXISTS /*_*/GooglePlaces (
-       `request` varchar(128) NOT NULL,
-       `response` mediumtext NOT NULL,
-       `expiration` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
-       UNIQUE KEY `request` (`request`)
-) /*$wgDBTableOptions*/;
diff --git a/GooglePlacesCache.php b/GooglePlacesCache.php
index a01d0b0..84d2eb6 100644
--- a/GooglePlacesCache.php
+++ b/GooglePlacesCache.php
@@ -1,55 +1,44 @@
 <?php
 
 /**
- * Custom db cache for Google Places calls
+ * Cache for Google Places calls
  *
  * @author Ike Hecht
  */
 class GooglePlacesCache {
-       const TABLE = 'GooglePlaces';
 
        /**
         * Get this call from db cache
         *
-        * @param string $request
+        * @param string $APIKey
+        * @param string $placeID
         * @return string|boolean
         */
-       public static function getCache( $request ) {
-               $dbr = wfGetDB( DB_SLAVE );
-               /** @todo Is this platform independent? */
-               $conds = array( 'request' => md5( serialize( $request ) ), 
$dbr->encodeExpiry( wfTimestampNow() ) . ' < expiration' );
-               $result = $dbr->select( self::TABLE, 'response', $conds, 
__METHOD__ );
-
-               $row = $result->fetchObject();
-               if ( $row ) {
-                       return ( unserialize( $row->response ) );
-               } else {
-                       return false;
-               }
+       public static function getCache( $APIKey, $placeID ) {
+               $cache = wfGetCache( CACHE_ANYTHING );
+               $key = wfMemcKey( 'googleplaces', $APIKey, $placeID );
+               $cached = $cache->get( $key );
+               wfDebugLog( "GooglePlaces",
+                       __METHOD__ . ": got " . var_export( $cached, true ) .
+                       " from cache." );
+               return $cached;
        }
 
        /**
         * Store this call in cache
         *
-        * @param string $request
+        * @param string $APIKey
+        * @param string $placeID
         * @param string $response
         * @param integer $cache_expire
         * @return boolean
-        * @throws MWException
         */
-       public static function setCache( $request, $response, $cache_expire ) {
-               /** @todo: cleanup expired cache rows */
-               $dbw = wfGetDB( DB_MASTER );
-               $data = array(
-                       'request' => md5( serialize( $request ) ),
-                       'response' => serialize( $response ),
-                       'expiration' => $dbw->encodeExpiry( wfTimestamp( TS_MW, 
time() + $cache_expire ) )
-               );
-               $result = $dbw->upsert( self::TABLE, $data, array( 'request' ), 
$data, __METHOD__ );
-               if ( !$result ) {
-                       throw new MWException( 'Set Cache failed' );
-               }
-
-               return $result;
+       public static function setCache( $APIKey, $placeID, $response, 
$cache_expire = 0 ) {
+               $cache = wfGetCache( CACHE_ANYTHING );
+               $key = wfMemcKey( 'googleplaces', $APIKey, $placeID );
+               wfDebugLog( "GooglePlaces",
+                       __METHOD__ . ": caching " . var_export( $response, true 
) .
+                       " from Google." );
+               return $cache->set( $key, $response, $cache_expire );
        }
 }
diff --git a/extension.json b/extension.json
index 1005f3d..f6b07b0 100644
--- a/extension.json
+++ b/extension.json
@@ -3,7 +3,7 @@
        "author": "Ike Hecht",
        "url": "https://www.mediawiki.org/wiki/Extension:GooglePlaces";,
        "descriptionmsg": "googleplaces-desc",
-       "version": "0.1",
+       "version": "1.0",
        "type": "parserhook",
        "AutoloadClasses": {
                "GooglePlacesHooks": "GooglePlaces.hooks.php",
@@ -17,9 +17,6 @@
        "Hooks": {
                "ParserFirstCallInit": [
                        "GooglePlacesHooks::onParserFirstCallInit"
-               ],
-               "LoadExtensionSchemaUpdates": [
-                       "GooglePlacesHooks::onLoadExtensionSchemaUpdates"
                ]
        },
        "MessagesDirs": {

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic51b5c223d5bc0779a47e3cc90b612d6c3688404
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/GooglePlaces
Gerrit-Branch: master
Gerrit-Owner: tosfos <[email protected]>

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

Reply via email to