Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
89.29% covered (warning)
89.29%
25 / 28
75.00% covered (warning)
75.00%
6 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
CachingSiteStore
92.59% covered (success)
92.59%
25 / 27
75.00% covered (warning)
75.00%
6 / 8
12.06
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getCacheKey
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getSites
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 getSite
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 saveSite
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 saveSites
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
2.03
 reset
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 clear
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace MediaWiki\Site;
8
9use Wikimedia\ObjectCache\BagOStuff;
10
11/**
12 * Wrap SiteList with an in-process cache and (optionally) a local-server cache.
13 *
14 * @internal For use by core ServiceWiring only. The public interface is SiteStore
15 * @ingroup Site
16 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
17 * @author Katie Filbert < aude.wiki@gmail.com >
18 */
19class CachingSiteStore implements SiteStore {
20    /** @var SiteStore */
21    private $siteStore;
22    /** @var BagOStuff */
23    private $cache;
24    /** @var string|null */
25    private $cacheKey = null;
26    /** @var SiteList|null */
27    private $sites = null;
28
29    public function __construct(
30        SiteStore $siteStore,
31        BagOStuff $cache
32    ) {
33        $this->siteStore = $siteStore;
34        $this->cache = $cache;
35    }
36
37    /**
38     * Constructs a cache key to use for caching the list of sites.
39     *
40     * This includes the concrete class name of the site list as well as a version identifier
41     * for the list's serialization, to avoid problems when unserializing site lists serialized
42     * by an older version, e.g. when reading from a cache.
43     *
44     * The cache key also includes information about where the sites were loaded from, e.g.
45     * the name of a database table.
46     *
47     * @see SiteList::getSerialVersionId
48     *
49     * @return string The cache key.
50     */
51    private function getCacheKey() {
52        if ( $this->cacheKey === null ) {
53            $version = SiteList::getSerialVersionId();
54            $this->cacheKey = $this->cache->makeKey( 'site-SiteList', $version );
55        }
56
57        return $this->cacheKey;
58    }
59
60    /**
61     * @see SiteStore::getSites
62     *
63     * @since 1.25
64     * @return SiteList
65     */
66    public function getSites() {
67        if ( $this->sites === null ) {
68            $this->sites = $this->cache->getWithSetCallback(
69                $this->getCacheKey(),
70                BagOStuff::TTL_HOUR,
71                function () {
72                    return $this->siteStore->getSites();
73                }
74            );
75        }
76
77        return $this->sites;
78    }
79
80    /**
81     * @see SiteStore::getSite
82     *
83     * @since 1.25
84     * @param string $globalId
85     * @return Site|null
86     */
87    public function getSite( $globalId ) {
88        $sites = $this->getSites();
89
90        return $sites->hasSite( $globalId ) ? $sites->getSite( $globalId ) : null;
91    }
92
93    /**
94     * @see SiteStore::saveSite
95     *
96     * @since 1.25
97     * @param Site $site
98     * @return bool Success indicator
99     */
100    public function saveSite( Site $site ) {
101        return $this->saveSites( [ $site ] );
102    }
103
104    /**
105     * @see SiteStore::saveSites
106     *
107     * @since 1.25
108     * @param Site[] $sites
109     * @return bool Success indicator
110     */
111    public function saveSites( array $sites ) {
112        if ( !$sites ) {
113            return true;
114        }
115
116        $success = $this->siteStore->saveSites( $sites );
117
118        // purge cache
119        $this->reset();
120
121        return $success;
122    }
123
124    /**
125     * Purge the internal and external cache of the site list, forcing the list.
126     * of sites to be reloaded.
127     *
128     * @since 1.25
129     */
130    public function reset() {
131        $this->cache->delete( $this->getCacheKey() );
132        $this->sites = null;
133    }
134
135    /**
136     * Clears the list of sites stored.
137     *
138     * NOTE: The fact that this also clears the in-process cache is an internal
139     * detail for PHPUnit testing only. The injected cache is generally APCU,
140     * which is per-server, so the cache reset would not apply to any other web servers.
141     *
142     * @see SiteStore::clear()
143     * @return bool Success
144     */
145    public function clear() {
146        $this->reset();
147
148        return $this->siteStore->clear();
149    }
150
151}
152
153/** @deprecated class alias since 1.42 */
154class_alias( CachingSiteStore::class, 'CachingSiteStore' );