| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\User; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class ActorCache { |
| 37 |
|
| 38 |
|
| 39 |
public const KEY_ACTOR_ID = 'actorId'; |
| 40 |
|
| 41 |
|
| 42 |
public const KEY_USER_ID = 'userId'; |
| 43 |
|
| 44 |
|
| 45 |
public const KEY_USER_NAME = 'name'; |
| 46 |
|
| 47 |
private int $maxSize; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
private $cache = [ self::KEY_ACTOR_ID => [], self::KEY_USER_NAME => [], self::KEY_USER_ID => [] ]; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public function __construct( int $maxSize ) { |
| 60 |
$this->maxSize = $maxSize; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public function getActor( string $keyType, $keyValue ): ?UserIdentity { |
| 70 |
return $this->getCachedValue( $keyType, $keyValue )['actor'] ?? null; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public function getActorId( string $keyType, $keyValue ): ?int { |
| 80 |
return $this->getCachedValue( $keyType, $keyValue )['actorId'] ?? null; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public function add( int $actorId, UserIdentity $actor ) { |
| 89 |
while ( count( $this->cache[self::KEY_ACTOR_ID] ) >= $this->maxSize ) { |
| 90 |
$evictId = array_key_first( $this->cache[self::KEY_ACTOR_ID] ); |
| 91 |
$this->remove( $this->cache[self::KEY_ACTOR_ID][$evictId]['actor'] ); |
| 92 |
} |
| 93 |
$value = [ 'actorId' => $actorId, 'actor' => $actor ]; |
| 94 |
$this->cache[self::KEY_ACTOR_ID][$actorId] = $value; |
| 95 |
$userId = $actor->getId( $actor->getWikiId() ); |
| 96 |
if ( $userId ) { |
| 97 |
$this->cache[self::KEY_USER_ID][$userId] = $value; |
| 98 |
} |
| 99 |
$this->cache[self::KEY_USER_NAME][$actor->getName()] = $value; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public function remove( UserIdentity $actor ) { |
| 107 |
$oldByName = $this->cache[self::KEY_USER_NAME][$actor->getName()] ?? null; |
| 108 |
$oldByUserId = $this->cache[self::KEY_USER_ID][$actor->getId( $actor->getWikiId() )] ?? null; |
| 109 |
if ( $oldByName ) { |
| 110 |
unset( $this->cache[self::KEY_USER_ID][$oldByName['actor']->getId( $oldByName['actor']->getWikiId() )] ); |
| 111 |
unset( $this->cache[self::KEY_ACTOR_ID][$oldByName['actorId']] ); |
| 112 |
} |
| 113 |
if ( $oldByUserId ) { |
| 114 |
unset( $this->cache[self::KEY_USER_NAME][$oldByUserId['actor']->getName()] ); |
| 115 |
unset( $this->cache[self::KEY_ACTOR_ID][$oldByUserId['actorId']] ); |
| 116 |
} |
| 117 |
unset( $this->cache[self::KEY_USER_NAME][$actor->getName()] ); |
| 118 |
unset( $this->cache[self::KEY_USER_ID][$actor->getId( $actor->getWikiId() )] ); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public function clear() { |
| 126 |
$this->cache = [ self::KEY_ACTOR_ID => [], self::KEY_USER_NAME => [], self::KEY_USER_ID => [] ]; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
private function getCachedValue( string $keyType, $keyValue ): ?array { |
| 135 |
if ( isset( $this->cache[$keyType][$keyValue] ) ) { |
| 136 |
$cached = $this->cache[$keyType][$keyValue]; |
| 137 |
$actorId = $cached['actorId']; |
| 138 |
|
| 139 |
$item = $this->cache[self::KEY_ACTOR_ID][$actorId]; |
| 140 |
unset( $this->cache[self::KEY_ACTOR_ID][$actorId] ); |
| 141 |
$this->cache[self::KEY_ACTOR_ID][$actorId] = $item; |
| 142 |
return $cached; |
| 143 |
} |
| 144 |
return null; |
| 145 |
} |
| 146 |
} |