MediaWiki  master
ActorCache.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\User;
22 
36 class ActorCache {
37 
39  public const KEY_ACTOR_ID = 'actorId';
40 
42  public const KEY_USER_ID = 'userId';
43 
45  public const KEY_USER_NAME = 'name';
46 
48  private $maxSize;
49 
55  private $cache = [ self::KEY_ACTOR_ID => [], self::KEY_USER_NAME => [], self::KEY_USER_ID => [] ];
56 
60  public function __construct( int $maxSize ) {
61  $this->maxSize = $maxSize;
62  }
63 
70  public function getActor( string $keyType, $keyValue ): ?UserIdentity {
71  return $this->getCachedValue( $keyType, $keyValue )['actor'] ?? null;
72  }
73 
80  public function getActorId( string $keyType, $keyValue ): ?int {
81  return $this->getCachedValue( $keyType, $keyValue )['actorId'] ?? null;
82  }
83 
89  public function add( int $actorId, UserIdentity $actor ) {
90  while ( count( $this->cache[self::KEY_ACTOR_ID] ) >= $this->maxSize ) {
91  reset( $this->cache[self::KEY_ACTOR_ID] );
92  $evictId = key( $this->cache[self::KEY_ACTOR_ID] );
93  $this->remove( $this->cache[self::KEY_ACTOR_ID][$evictId]['actor'] );
94  }
95  $value = [ 'actorId' => $actorId, 'actor' => $actor ];
96  $this->cache[self::KEY_ACTOR_ID][$actorId] = $value;
97  $userId = $actor->getId( $actor->getWikiId() );
98  if ( $userId ) {
99  $this->cache[self::KEY_USER_ID][$userId] = $value;
100  }
101  $this->cache[self::KEY_USER_NAME][$actor->getName()] = $value;
102  }
103 
108  public function remove( UserIdentity $actor ) {
109  $oldByName = $this->cache[self::KEY_USER_NAME][$actor->getName()] ?? null;
110  $oldByUserId = $this->cache[self::KEY_USER_ID][$actor->getId( $actor->getWikiId() )] ?? null;
111  if ( $oldByName ) {
112  unset( $this->cache[self::KEY_USER_ID][$oldByName['actor']->getId( $oldByName['actor']->getWikiId() )] );
113  unset( $this->cache[self::KEY_ACTOR_ID][$oldByName['actorId']] );
114  }
115  if ( $oldByUserId ) {
116  unset( $this->cache[self::KEY_USER_NAME][$oldByUserId['actor']->getName()] );
117  unset( $this->cache[self::KEY_ACTOR_ID][$oldByUserId['actorId']] );
118  }
119  unset( $this->cache[self::KEY_USER_NAME][$actor->getName()] );
120  unset( $this->cache[self::KEY_USER_ID][$actor->getId( $actor->getWikiId() )] );
121  }
122 
127  public function clear() {
128  $this->cache = [ self::KEY_ACTOR_ID => [], self::KEY_USER_NAME => [], self::KEY_USER_ID => [] ];
129  }
130 
136  private function getCachedValue( string $keyType, $keyValue ): ?array {
137  if ( isset( $this->cache[$keyType][$keyValue] ) ) {
138  $cached = $this->cache[$keyType][$keyValue];
139  $this->ping( $cached['actorId'] );
140  return $cached;
141  }
142  return null;
143  }
144 
149  private function ping( int $actorId ) {
150  $item = $this->cache[self::KEY_ACTOR_ID][$actorId];
151  unset( $this->cache[self::KEY_ACTOR_ID][$actorId] );
152  $this->cache[self::KEY_ACTOR_ID][$actorId] = $item;
153  }
154 }
MediaWiki\User\UserIdentity\getId
getId( $wikiId=self::LOCAL)
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
MediaWiki\User\ActorCache\clear
clear()
Remove everything from the cache.
Definition: ActorCache.php:127
MediaWiki\User\ActorCache\$maxSize
int $maxSize
Definition: ActorCache.php:44
MediaWiki\User\UserIdentity\getName
getName()
MediaWiki\User\ActorCache
Definition: ActorCache.php:36
MediaWiki\DAO\WikiAwareEntity\getWikiId
getWikiId()
Get the ID of the wiki this page belongs to.
MediaWiki\User\ActorCache\getActor
getActor(string $keyType, $keyValue)
Get user identity which has $keyType equal to $keyValue.
Definition: ActorCache.php:70
MediaWiki\User
Definition: ActorCache.php:21
MediaWiki\User\ActorCache\add
add(int $actorId, UserIdentity $actor)
Add $actor with $actorId to the cache.
Definition: ActorCache.php:89
MediaWiki\User\ActorCache\ping
ping(int $actorId)
Record the actor with $actorId was recently used.
Definition: ActorCache.php:149
MediaWiki\User\ActorCache\getCachedValue
getCachedValue(string $keyType, $keyValue)
Definition: ActorCache.php:136
MediaWiki\User\ActorCache\$cache
array[][] $cache
Contains 3 keys, KEY_ACTOR_ID, KEY_USER_ID, and KEY_USER_NAME, each of which has a value of an array ...
Definition: ActorCache.php:55
MediaWiki\User\ActorCache\getActorId
getActorId(string $keyType, $keyValue)
Get actor ID of the user which has $keyType equal to $keyValue.
Definition: ActorCache.php:80
MediaWiki\User\ActorCache\__construct
__construct(int $maxSize)
Definition: ActorCache.php:60