Skip to content

Commit

Permalink
ArticlePurge add safeguard to flush query result cache (#2438)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwjames committed Apr 29, 2017
1 parent a7b1681 commit c6a3ebe
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 7 deletions.
7 changes: 7 additions & 0 deletions src/EventListenerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ private function addListenersToCollection() {
$context
);

if ( $dispatchContext->has( 'ask' ) ) {
$applicationFactory->singleton( 'CachedQueryResultPrefetcher' )->resetCacheBy(
$dispatchContext->get( 'ask' ),
$context
);
}

$dispatchContext->set( 'propagationstop', true );
}
);
Expand Down
7 changes: 7 additions & 0 deletions src/MediaWiki/Hooks/ArticlePurge.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use SMW\ApplicationFactory;
use SMW\EventHandler;
use SMW\DIWikiPage;
use SMW\DIProperty;
use WikiPage;

/**
Expand Down Expand Up @@ -56,6 +57,12 @@ public function process( WikiPage &$wikiPage ) {
}

if ( $settings->get( 'smwgQueryResultCacheRefreshOnPurge' ) ) {

$dispatchContext->set( 'ask', $applicationFactory->getStore()->getPropertyValues(
DIWikiPage::newFromTitle( $wikiPage->getTitle() ),
new DIProperty( '_ASK') )
);

EventHandler::getInstance()->getEventDispatcher()->dispatch(
'cached.prefetcher.reset',
$dispatchContext
Expand Down
23 changes: 16 additions & 7 deletions src/Query/Result/CachedQueryResultPrefetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,20 +283,24 @@ public function getQueryResult( Query $query ) {
/**
* @since 2.5
*
* @param DIWikiPage|array $list
* @param DIWikiPage|array $items
* @param string $context
*/
public function resetCacheBy( $item, $context = '' ) {
public function resetCacheBy( $items, $context = '' ) {

if ( !is_array( $item ) ) {
$item = array( $item );
if ( !$this->blobStore->canUse() ) {
return;
}

if ( !is_array( $items ) ) {
$items = array( $items );
}

$recordStats = false;
$context = $context === '' ? 'Undefined' : $context;

foreach ( $item as $id ) {
$id = $this->getHashFrom( $id );
foreach ( $items as $item ) {
$id = $this->getHashFrom( $item );
$this->tempCache->delete( $id );

if ( $this->blobStore->exists( $id ) ) {
Expand Down Expand Up @@ -462,7 +466,12 @@ private function addToLinkedList( $contextPage, $queryId ) {
private function getHashFrom( $subject ) {

if ( $subject instanceof DIWikiPage ) {
$subject = $subject->asBase()->getHash();
// In case the we detect a _QUERY subobject, use it directly
if ( ( $subobjectName = $subject->getSubobjectName() ) !== '' && strpos( $subobjectName, Query::ID_PREFIX ) !== false ) {
$subject = $subobjectName;
} else {
$subject = $subject->asBase()->getHash();
}
}

return md5( $subject . self::VERSION . $this->hashModifier );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ public function testGetQueryResultFromTempCache() {

public function testPurgeCacheByQueryList() {

$this->blobStore->expects( $this->atLeastOnce() )
->method( 'canUse' )
->will( $this->returnValue( true ) );

$this->blobStore->expects( $this->atLeastOnce() )
->method( 'exists' )
->will( $this->returnValue( true ) );
Expand Down Expand Up @@ -211,6 +215,10 @@ public function testPurgeCacheBySubject() {

$subject = new DIWikiPage( 'Foo', NS_MAIN );

$this->blobStore->expects( $this->atLeastOnce() )
->method( 'canUse' )
->will( $this->returnValue( true ) );

$this->blobStore->expects( $this->atLeastOnce() )
->method( 'exists' )
->will( $this->returnValue( true ) );
Expand All @@ -232,6 +240,44 @@ public function testPurgeCacheBySubject() {
$instance->resetCacheBy( $subject );
}

public function testPurgeCacheBySubjectWith_QUERY() {

$subject = $this->getMockBuilder( '\SMW\DIWikiPage' )
->disableOriginalConstructor()
->getMock();

$subject->expects( $this->atLeastOnce() )
->method( 'getSubobjectName' )
->will( $this->returnValue( '_QUERYfoo' ) );

$subject->expects( $this->never() )
->method( 'asBase' );

$this->blobStore->expects( $this->atLeastOnce() )
->method( 'canUse' )
->will( $this->returnValue( true ) );

$this->blobStore->expects( $this->atLeastOnce() )
->method( 'exists' )
->will( $this->returnValue( true ) );

$this->blobStore->expects( $this->atLeastOnce() )
->method( 'delete' )
->with( $this->equalTo( 'dc63f8b4cab1bb1214979932b637cdec' ) );

$this->bufferedStatsdCollector->expects( $this->once() )
->method( 'recordStats' );

$instance = new CachedQueryResultPrefetcher(
$this->store,
$this->queryFactory,
$this->blobStore,
$this->bufferedStatsdCollector
);

$instance->resetCacheBy( $subject );
}

public function testGetStats() {

$stats = array(
Expand Down

0 comments on commit c6a3ebe

Please sign in to comment.