mediawiki-extensions-Echo/includes/SeenTime.php
Kunal Mehta 35c4a37918 Use db-replicated objectcache for storing last seen time
So we're not abusing user preferences for the last seen time.
EchoSeenTime is a small wrapper around ObjectCache that handles the
fallback to user preferences during the transition.

All JavaScript code now needs to use mw.config.get('wgEchoSeenTime').

Bug: T95839
Change-Id: Ia45ba5e30eb4564250539d04d5886d2598ebd49a
2015-09-02 22:42:35 +00:00

60 lines
1.2 KiB
PHP

<?php
/**
* A small wrapper around ObjectCache to manage
* storing the last time a user has seen notifications
*/
class EchoSeenTime {
/**
* @var User
*/
private $user;
/**
* @var string
*/
private $key;
/**
* @var BagOStuff
*/
private $cache;
/**
* @param User $user A logged in user
*/
private function __construct( User $user ) {
$this->user = $user;
$this->key = wfMemcKey( 'echo', 'seen', 'time', $user->getId() );
$this->cache = ObjectCache::getInstance( 'db-replicated' );
}
/**
* @param User $user
* @return EchoSeenTime
*/
public static function newFromUser( User $user ) {
return new self( $user );
}
/**
* @param int $flags BagOStuff::READ_LATEST to use the master
* @return string|bool false if no stored time
*/
public function getTime( $flags = 0 ) {
$cas = 0; // Unused, but we have to pass something by reference
$data = $this->cache->get( $this->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 );
}
return $data;
}
public function setTime( $time ) {
return $this->cache->set( $this->key, $time );
}
}