Code style/uniformity cleanups to GadgetDefinitionNamespaceRepo

Change-Id: Ic49383ac1ef99494050005c3641aa6a758fe3e8c
This commit is contained in:
Aaron Schulz 2017-03-02 17:41:25 -08:00
parent a0e70a5d24
commit 5fd412b41d
4 changed files with 47 additions and 39 deletions

View file

@ -1,12 +1,13 @@
<?php
use MediaWiki\MediaWikiServices;
/**
* GadgetRepo implementation where each gadget has a page in
* the Gadget definition namespace, and scripts and styles are
* located in the Gadget namespace.
*/
class GadgetDefinitionNamespaceRepo extends GadgetRepo {
/**
* How long in seconds the list of gadget ids and
* individual gadgets should be cached for (1 day)
@ -18,22 +19,8 @@ class GadgetDefinitionNamespaceRepo extends GadgetRepo {
*/
private $wanCache;
/**
* @var string
*/
private $idsKey;
public function __construct () {
$this->idsKey = wfMemcKey( 'gadgets', 'namespace', 'ids' );
$this->wanCache = ObjectCache::getMainWANInstance();
}
/**
* Purge the list of gadget ids when a page is deleted
* or if a new page is created
*/
public function purgeGadgetIdsList() {
$this->wanCache->touchCheckKey( $this->idsKey );
$this->wanCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
}
/**
@ -42,40 +29,35 @@ class GadgetDefinitionNamespaceRepo extends GadgetRepo {
* @return string[]
*/
public function getGadgetIds() {
$key = $this->getGadgetIdsKey();
return $this->wanCache->getWithSetCallback(
$this->idsKey,
$key,
self::CACHE_TTL,
function( $oldValue, &$ttl, array &$setOpts ) {
function ( $oldValue, &$ttl, array &$setOpts ) {
$dbr = wfGetDB( DB_SLAVE );
$setOpts += Database::getCacheSetOptions( $dbr );
return $dbr->selectFieldValues(
'page',
'page_title',
[
'page_namespace' => NS_GADGET_DEFINITION
],
[ 'page_namespace' => NS_GADGET_DEFINITION ],
__METHOD__
);
},
[
'checkKeys' => [ $this->idsKey ],
'pcTTL' => 5,
'lockTSE' => '30',
'checkKeys' => [ $key ],
'pcTTL' => WANObjectCache::TTL_PROC_SHORT,
'lockTSE' => 30
]
);
}
/**
* Update the cache for a specific Gadget whenever it is updated
*
* @param string $id
* Purge the list of gadget ids when a page is deleted or if a new page is created
*/
public function updateGadgetObjectCache( $id ) {
$this->wanCache->touchCheckKey( $this->getGadgetCacheKey( $id ) );
}
private function getGadgetCacheKey( $id ) {
return wfMemcKey( 'gadgets', 'object', md5( $id ), Gadget::GADGET_CLASS_VERSION );
public function purgeGadgetIdsList() {
$this->wanCache->touchCheckKey( $this->getGadgetIdsKey() );
}
/**
@ -88,13 +70,14 @@ class GadgetDefinitionNamespaceRepo extends GadgetRepo {
$gadget = $this->wanCache->getWithSetCallback(
$key,
self::CACHE_TTL,
function( $old, &$ttl, array &$setOpts ) use ( $id ) {
function ( $old, &$ttl, array &$setOpts ) use ( $id ) {
$setOpts += Database::getCacheSetOptions( wfGetDB( DB_SLAVE ) );
$title = Title::makeTitleSafe( NS_GADGET_DEFINITION, $id );
if ( !$title ) {
$ttl = WANObjectCache::TTL_UNCACHEABLE;
return null;
}
$rev = Revision::newFromTitle( $title );
if ( !$rev ) {
$ttl = WANObjectCache::TTL_UNCACHEABLE;
@ -112,8 +95,8 @@ class GadgetDefinitionNamespaceRepo extends GadgetRepo {
},
[
'checkKeys' => [ $key ],
'pcTTL' => 5,
'lockTSE' => '30',
'pcTTL' => WANObjectCache::TTL_PROC_SHORT,
'lockTSE' => 30
]
);
@ -123,4 +106,29 @@ class GadgetDefinitionNamespaceRepo extends GadgetRepo {
return $gadget;
}
/**
* Update the cache for a specific Gadget whenever it is updated
*
* @param string $id
*/
public function purgeGadgetEntry( $id ) {
$this->wanCache->touchCheckKey( $this->getGadgetCacheKey( $id ) );
}
/**
* @return string
*/
private function getGadgetIdsKey() {
return $this->wanCache->makeKey( 'gadgets', 'namespace', 'ids' );
}
/**
* @param $id
* @return string
*/
private function getGadgetCacheKey( $id ) {
return $this->wanCache->makeKey(
'gadgets', 'object', md5( $id ), Gadget::GADGET_CLASS_VERSION );
}
}

View file

@ -65,7 +65,7 @@ class GadgetResourceLoaderModule extends ResourceLoaderWikiModule {
/**
* Overrides ResourceLoaderModule::getDependencies()
* @param $context ResourceLoaderContext
* @return Array: Names of resources this module depends on
* @return string[] Names of resources this module depends on
*/
public function getDependencies( ResourceLoaderContext $context = null ) {
return $this->getGadget()->getDependencies();

View file

@ -39,7 +39,7 @@ class GadgetDefinitionDeletionUpdate extends DataUpdate {
$repo = GadgetRepo::singleton();
if ( $repo instanceof GadgetDefinitionNamespaceRepo ) {
$repo->purgeGadgetIdsList();
$repo->updateGadgetObjectCache( $this->id );
$repo->purgeGadgetEntry( $this->id );
}
}
}

View file

@ -31,7 +31,7 @@ class GadgetDefinitionSecondaryDataUpdate extends DataUpdate {
public function doUpdate() {
$repo = GadgetRepo::singleton();
if ( $repo instanceof GadgetDefinitionNamespaceRepo ) {
$repo->updateGadgetObjectCache( $this->id );
$repo->purgeGadgetEntry( $this->id );
}
}
}