3 use Psr\Log\LoggerInterface
;
6 * Class for fixing stale WANObjectCache keys using a purge event source
8 * This is useful for expiring keys that missed fire-and-forget purges. This uses the
9 * recentchanges table as a reliable stream to make certain keys reach consistency
10 * as soon as the underlying replica database catches up. These means that critical
11 * keys will not escape getting purged simply due to brief hiccups in the network,
12 * which are more prone to happen accross datacenters.
15 * "I was trying to cheat death. I was only trying to surmount for a little while the
16 * darkness that all my life I surely knew was going to come rolling in on me some day
17 * and obliterate me. I was only to stay alive a little brief while longer, after I was
18 * already gone. To stay in the light, to be with the living, a little while past my time."
19 * -- Notes for "Blues of a Lifetime", by [[Cornell Woolrich]]
23 class WANCacheReapUpdate
implements DeferrableUpdate
{
26 /** @var LoggerInterface */
30 * @param IDatabase $db
31 * @param LoggerInterface $logger
33 public function __construct( IDatabase
$db, LoggerInterface
$logger ) {
35 $this->logger
= $logger;
39 $reaper = new WANObjectCacheReaper(
40 ObjectCache
::getMainWANInstance(),
41 ObjectCache
::getLocalClusterInstance(),
42 [ $this, 'getTitleChangeEvents' ],
43 [ $this, 'getEventAffectedKeys' ],
45 'channel' => 'table:recentchanges:' . $this->db
->getWikiID(),
46 'logger' => $this->logger
50 $reaper->invoke( 100 );
54 * @see WANObjectCacheRepear
60 * @return TitleValue[]
62 public function getTitleChangeEvents( $start, $id, $end, $limit ) {
64 $encStart = $db->addQuotes( $db->timestamp( $start ) );
65 $encEnd = $db->addQuotes( $db->timestamp( $end ) );
66 $id = (int)$id; // cast NULL => 0 since rc_id is an integer
70 [ 'rc_namespace', 'rc_title', 'rc_timestamp', 'rc_id' ],
73 "rc_timestamp > $encStart",
74 "rc_timestamp = $encStart AND rc_id > " . $db->addQuotes( $id )
76 "rc_timestamp < $encEnd"
79 [ 'ORDER BY' => 'rc_timestamp ASC, rc_id ASC', 'LIMIT' => $limit ]
83 foreach ( $res as $row ) {
85 'id' => (int)$row->rc_id
,
86 'pos' => (int)wfTimestamp( TS_UNIX
, $row->rc_timestamp
),
87 'item' => new TitleValue( (int)$row->rc_namespace
, $row->rc_title
)
95 * Gets a list of important cache keys associated with a title
97 * @see WANObjectCacheRepear
98 * @param WANObjectCache $cache
99 * @param TitleValue $t
102 public function getEventAffectedKeys( WANObjectCache
$cache, TitleValue
$t ) {
103 /** @var WikiPage[]|LocalFile[]|User[] $entities */
106 $entities[] = WikiPage
::factory( Title
::newFromTitleValue( $t ) );
107 if ( $t->inNamespace( NS_FILE
) ) {
108 $entities[] = wfLocalFile( $t->getText() );
110 if ( $t->inNamespace( NS_USER
) ) {
111 $entities[] = User
::newFromName( $t->getText(), false );
115 foreach ( $entities as $entity ) {
117 $keys = array_merge( $keys, $entity->getMutableCacheKeys( $cache ) );
121 $this->logger
->debug( __CLASS__
. ': got key(s) ' . implode( ', ', $keys ) );