Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.77% covered (success)
96.77%
30 / 31
85.71% covered (warning)
85.71%
6 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ActorCache
96.77% covered (success)
96.77%
30 / 31
85.71% covered (warning)
85.71%
6 / 7
12
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getActor
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getActorId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 add
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 remove
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
 clear
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCachedValue
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace MediaWiki\User;
8
9/**
10 * Simple in-memory cache for UserIdentity objects indexed by user ID,
11 * actor ID and user name.
12 *
13 * We cannot just use MapCacheLRU for this because of eviction semantics:
14 * we need to be able to remove UserIdentity from the cache even if
15 * user ID or user name has changed, so we track the most accessed VALUES
16 * in the cache, not keys, and evict them alongside with all their indexes.
17 *
18 * @since 1.37
19 * @internal For use by ActorStore
20 * @ingroup User
21 */
22class ActorCache {
23
24    /** @var string Key by actor ID */
25    public const KEY_ACTOR_ID = 'actorId';
26
27    /** @var string Key by user ID */
28    public const KEY_USER_ID = 'userId';
29
30    /** @var string Key by user name */
31    public const KEY_USER_NAME = 'name';
32
33    /**
34     * @var array[][] Contains 3 keys, KEY_ACTOR_ID, KEY_USER_ID, and KEY_USER_NAME,
35     * each of which has a value of an array of arrays with actor ids and UserIdentity objects,
36     * keyed with the corresponding actor id/user id/user name
37     */
38    private $cache = [ self::KEY_ACTOR_ID => [], self::KEY_USER_NAME => [], self::KEY_USER_ID => [] ];
39
40    /**
41     * @param int $maxSize hold up to this many UserIdentity values
42     */
43    public function __construct(
44        private readonly int $maxSize,
45    ) {
46    }
47
48    /**
49     * Get user identity which has $keyType equal to $keyValue
50     * @param string $keyType one of self::KEY_* constants.
51     * @param string|int $keyValue
52     * @return UserIdentity|null
53     */
54    public function getActor( string $keyType, $keyValue ): ?UserIdentity {
55        return $this->getCachedValue( $keyType, $keyValue )['actor'] ?? null;
56    }
57
58    /**
59     * Get actor ID of the user which has $keyType equal to $keyValue.
60     * @param string $keyType one of self::KEY_* constants.
61     * @param string|int $keyValue
62     * @return int|null
63     */
64    public function getActorId( string $keyType, $keyValue ): ?int {
65        return $this->getCachedValue( $keyType, $keyValue )['actorId'] ?? null;
66    }
67
68    /**
69     * Add $actor with $actorId to the cache.
70     * @param int $actorId
71     * @param UserIdentity $actor
72     */
73    public function add( int $actorId, UserIdentity $actor ) {
74        while ( count( $this->cache[self::KEY_ACTOR_ID] ) >= $this->maxSize ) {
75            $evictId = array_key_first( $this->cache[self::KEY_ACTOR_ID] );
76            $this->remove( $this->cache[self::KEY_ACTOR_ID][$evictId]['actor'] );
77        }
78        $value = [ 'actorId' => $actorId, 'actor' => $actor ];
79        $this->cache[self::KEY_ACTOR_ID][$actorId] = $value;
80        $userId = $actor->getId( $actor->getWikiId() );
81        if ( $userId ) {
82            $this->cache[self::KEY_USER_ID][$userId] = $value;
83        }
84        $this->cache[self::KEY_USER_NAME][$actor->getName()] = $value;
85    }
86
87    public function remove( UserIdentity $actor ) {
88        $oldByName = $this->cache[self::KEY_USER_NAME][$actor->getName()] ?? null;
89        $oldByUserId = $this->cache[self::KEY_USER_ID][$actor->getId( $actor->getWikiId() )] ?? null;
90        if ( $oldByName ) {
91            unset( $this->cache[self::KEY_USER_ID][$oldByName['actor']->getId( $oldByName['actor']->getWikiId() )] );
92            unset( $this->cache[self::KEY_ACTOR_ID][$oldByName['actorId']] );
93        }
94        if ( $oldByUserId ) {
95            unset( $this->cache[self::KEY_USER_NAME][$oldByUserId['actor']->getName()] );
96            unset( $this->cache[self::KEY_ACTOR_ID][$oldByUserId['actorId']] );
97        }
98        unset( $this->cache[self::KEY_USER_NAME][$actor->getName()] );
99        unset( $this->cache[self::KEY_USER_ID][$actor->getId( $actor->getWikiId() )] );
100    }
101
102    /**
103     * Remove everything from the cache.
104     * @internal
105     */
106    public function clear() {
107        $this->cache = [ self::KEY_ACTOR_ID => [], self::KEY_USER_NAME => [], self::KEY_USER_ID => [] ];
108    }
109
110    /**
111     * @param string $keyType one of self::KEY_* constants.
112     * @param string|int $keyValue
113     * @return array|null [ 'actor' => UserIdentity, 'actorId' => int ]
114     */
115    private function getCachedValue( string $keyType, $keyValue ): ?array {
116        if ( isset( $this->cache[$keyType][$keyValue] ) ) {
117            $cached = $this->cache[$keyType][$keyValue];
118            $actorId = $cached['actorId'];
119            // Record the actor with $actorId was recently used.
120            $item = $this->cache[self::KEY_ACTOR_ID][$actorId];
121            unset( $this->cache[self::KEY_ACTOR_ID][$actorId] );
122            $this->cache[self::KEY_ACTOR_ID][$actorId] = $item;
123            return $cached;
124        }
125        return null;
126    }
127}