Bsitu has uploaded a new change for review.

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

Change subject: [2] Add title local cache holder objects
......................................................................

[2] Add title local cache holder objects

Change-Id: I8fe767ac2669e67bdf7d17eecccfc0dcb6b5fc7d
---
M Echo.php
A includes/cache/LocalCache.php
A includes/cache/TitleLocalCache.php
A tests/includes/cache/TitleLocalCacheTest.php
4 files changed, 186 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Echo 
refs/changes/81/154381/1

diff --git a/Echo.php b/Echo.php
index 8fded26..ae9346e 100644
--- a/Echo.php
+++ b/Echo.php
@@ -67,6 +67,10 @@
 $wgAutoloadClasses['EchoTargetPageMapper'] = $dir . 
'includes/mapper/TargetPageMapper.php';
 $wgAutoloadClasses['EchoUserNotificationGateway'] = $dir . 
'includes/gateway/UserNotificationGateway.php';
 
+// Local caches
+$wgAutoloadClasses['EchoLocalCache'] = $dir . 'includes/cache/LocalCache.php';
+$wgAutoloadClasses['EchoTitleLocalCache'] = $dir . 
'includes/cache/TitleLocalCache.php';
+
 // Output formatters
 $wgAutoloadClasses['EchoDataOutputFormatter'] = $dir . 
'includes/DataOutputFormatter.php';
 
diff --git a/includes/cache/LocalCache.php b/includes/cache/LocalCache.php
new file mode 100644
index 0000000..361165c
--- /dev/null
+++ b/includes/cache/LocalCache.php
@@ -0,0 +1,78 @@
+<?php
+
+/**
+ * Base Local cache object, which borrows the concept from Flow user listener
+ */
+abstract class EchoLocalCache {
+
+       /**
+        * Target object cache
+        * @var array
+        */
+       protected $targets = array();
+
+       /**
+        * Lookup ids that have not been resolved for a target
+        * @param int[]
+        */
+       protected $lookups = array();
+       /**
+        * Resolve ids in lookups to targets
+        */
+       abstract protected function resolve();
+       
+       /**
+        * Add a key to the lookup and the key is used to resolve cache target
+        *
+        * @param int $key
+        */
+       public function add( $key, $target = null ) {
+               if ( !isset( $this->targets[$key] ) ) {
+                       $this->lookups[$key] = $key;
+               }
+       }
+
+       /**
+        * Get the cache target based on the key
+        *
+        * @param int $key
+        * @return mixed|null
+        */
+       public function get( $key ) {
+               if ( isset( $this->targets[$key] ) ) {
+                       return $this->targets[$key];
+               }
+               if ( isset( $this->lookups[$key] ) ) {
+                       $this->resolve();
+                       if ( isset( $this->targets[$key] ) ) {
+                               return $this->targets[$key];
+                       }
+               }
+               return null;
+       }
+
+       /**
+        * Clear everything in local cache
+        */
+       public function clearAll() {
+               foreach ( $this->targets as $id => $target ) {
+                       unset( $this->targets[$id] );
+               }
+               $this->lookups = array();
+       }
+
+       /**
+        * @return int[]
+        */
+       public function getLookups() {
+               return $this->lookups;  
+       }
+       
+       /**
+        * @return array
+        */
+       public function getTargets() {
+               return $this->targets;
+       }
+
+}
diff --git a/includes/cache/TitleLocalCache.php 
b/includes/cache/TitleLocalCache.php
new file mode 100644
index 0000000..5ee8069
--- /dev/null
+++ b/includes/cache/TitleLocalCache.php
@@ -0,0 +1,39 @@
+<?php
+
+class EchoTitleLocalCache extends EchoLocalCache {
+
+       /**
+        * @var EchoTitleLocalCache
+        */
+       private static $instance;
+
+       /**
+        * Use EchoTitleLocalCache::create()
+        */
+       private function __construct() {}
+
+       /**
+        * Create a TitleLocalCache object
+        * @return TitleLocalCache
+        */
+       public static function create() {
+               if ( !self::$instance ) {
+                       self::$instance = new EchoTitleLocalCache();
+               }
+               return self::$instance;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       protected function resolve() {
+               if ( $this->lookups ) {
+                       $titles = Title::newFromIDs( $this->lookups );
+                       foreach ( $titles as $title ) {
+                               $this->targets[$title->getArticleId()] = $title;
+                       }
+                       $this->lookups = array();
+               }
+       }
+
+}
diff --git a/tests/includes/cache/TitleLocalCacheTest.php 
b/tests/includes/cache/TitleLocalCacheTest.php
new file mode 100644
index 0000000..deb8c30
--- /dev/null
+++ b/tests/includes/cache/TitleLocalCacheTest.php
@@ -0,0 +1,65 @@
+<?php
+
+class EchoTitleLocalCacheTest extends MediaWikiTestCase {
+
+       public function testCreate() {
+               $cache = EchoTitleLocalCache::create();
+               $this->assertInstanceOf( 'EchoTitleLocalCache', $cache );
+               return $cache;
+       }
+
+       /**
+        * @depends testCreate
+        */
+       public function testAdd( $cache ) {
+               $cache->add( 1 );
+               $this->assertEquals( count( $cache->getLookups() ), 1 );
+               $this->assertArrayHasKey( 1, $cache->getLookups() );
+       }
+
+       /**
+        * @depends testCreate
+        */
+       public function testGet( $cache ) {
+               $object = new \ReflectionObject( $cache );
+               $targets = $object->getProperty( 'targets' );
+               $targets->setAccessible( true );
+               $targets->setValue( $cache, array( '1' => $this->mockTitle() ) 
);
+               $lookups = $object->getProperty( 'lookups' );
+               $lookups->setAccessible( true );
+               $lookups->setValue( $cache, array( '1' => '1', '2' => '2' ) );
+
+               $this->assertTrue( count( $cache->getLookups() ) > 0 );
+               $this->assertInstanceOf( 'Title', $cache->get( 1 ) );
+               $this->assertTrue( count( $cache->getLookups() ) > 0 );
+               $this->assertInstanceOf( 'Title', $cache->get( 2 ) );
+               $this->assertTrue( count( $cache->getLookups() ) == 0 );
+       }
+
+       /**
+        * @depends testCreate
+        */
+       public function testClearAll( $cache ) {
+               $object = new \ReflectionObject( $cache );
+               $targets = $object->getProperty( 'targets' );
+               $targets->setAccessible( true );
+               $targets->setValue( $cache, array( '1' => $this->mockTitle() ) 
);
+               $lookups = $object->getProperty( 'lookups' );
+               $lookups->setAccessible( true );
+               $lookups->setValue( $cache, array( '1' => '1', '2' => '2' ) );
+
+               $cache->clearAll();
+               $this->assertTrue( count( $cache->getLookups() ) == 0 );
+               $this->assertTrue( count( $cache->getTargets() ) == 0 );
+       }
+
+       /**
+        * Mock object of Title
+        */
+       protected function mockTitle() {
+               $title = $this->getMockBuilder( 'Title' )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+               return $title;
+       }
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8fe767ac2669e67bdf7d17eecccfc0dcb6b5fc7d
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Echo
Gerrit-Branch: master
Gerrit-Owner: Bsitu <bs...@wikimedia.org>

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

Reply via email to