Use static cache for times so it works across instances of SeenTime

Follows-up If6319a02. Making the cache static enables us to reuse
the in-process cache across multiple instances of the class.

Bug: T144534
Change-Id: I1a594830f13d56ab4e0e636a3cdb5e96379e990a
This commit is contained in:
Chad Horohoe 2016-09-08 12:22:24 -07:00
parent a04c10790a
commit da4f8f5db9

View file

@ -17,22 +17,11 @@ class EchoSeenTime {
*/
private $user;
/**
* @var BagOStuff
*/
private $cache;
/**
* @param User $user A logged in user
*/
private function __construct( User $user ) {
$this->user = $user;
// Use db-replicated for persistent storage, and
// wrap it with CachedBagOStuff for an in-process
// cache. (T144534)
$this->cache = new CachedBagOStuff(
ObjectCache::getInstance( 'db-replicated' )
);
}
/**
@ -43,6 +32,27 @@ class EchoSeenTime {
return new self( $user );
}
/**
* Hold onto a cache for our operations. Static so it can reuse the same
* in-process cache in different instances.
*
* @return BagOStuff
*/
private static function cache() {
static $c = null;
// Use db-replicated for persistent storage, and
// wrap it with CachedBagOStuff for an in-process
// cache. (T144534)
if ( $c === null ) {
$c = new CachedBagOStuff(
ObjectCache::getInstance( 'db-replicated' )
);
}
return $c;
}
/**
* @param string $type Type of seen time to get
* @param int $flags BagOStuff::READ_LATEST to use the master
@ -64,7 +74,7 @@ class EchoSeenTime {
if ( $this->validateType( $type ) ) {
$key = wfMemcKey( 'echo', 'seen', $type, 'time', $this->user->getId() );
$cas = 0; // Unused, but we have to pass something by reference
$data = $this->cache->get( $key, $cas, $flags );
$data = self::cache()->get( $key, $cas, $flags );
if ( $data === false ) {
// Check if the user still has it set in their preferences
$data = $this->user->getOption( 'echo-seen-time', false );
@ -94,7 +104,7 @@ class EchoSeenTime {
if ( $this->validateType( $type ) ) {
$key = wfMemcKey( 'echo', 'seen', $type, 'time', $this->user->getId() );
return $this->cache->set( $key, $time );
return self::cache()->set( $key, $time );
}
}
}