Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
89.29% |
25 / 28 |
|
75.00% |
6 / 8 |
CRAP | |
0.00% |
0 / 1 |
CachingSiteStore | |
92.59% |
25 / 27 |
|
75.00% |
6 / 8 |
12.06 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getCacheKey | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getSites | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
getSite | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
saveSite | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
saveSites | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
reset | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
clear | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | namespace MediaWiki\Site; |
22 | |
23 | use Wikimedia\ObjectCache\BagOStuff; |
24 | |
25 | /** |
26 | * Hold a configured list of sites (SiteList), with a caching layer. |
27 | * |
28 | * @internal For use by core ServiceWiring only. The public interface is SiteStore |
29 | * @ingroup Site |
30 | * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
31 | * @author Katie Filbert < aude.wiki@gmail.com > |
32 | */ |
33 | class CachingSiteStore implements SiteStore { |
34 | /** @var SiteStore */ |
35 | private $siteStore; |
36 | /** @var BagOStuff */ |
37 | private $cache; |
38 | |
39 | /** @var string|null */ |
40 | private $cacheKey = null; |
41 | /** @var SiteList|null */ |
42 | private $sites = null; |
43 | |
44 | /** |
45 | * @param SiteStore $siteStore |
46 | * @param BagOStuff $cache |
47 | */ |
48 | public function __construct( |
49 | SiteStore $siteStore, |
50 | BagOStuff $cache |
51 | ) { |
52 | $this->siteStore = $siteStore; |
53 | $this->cache = $cache; |
54 | } |
55 | |
56 | /** |
57 | * Constructs a cache key to use for caching the list of sites. |
58 | * |
59 | * This includes the concrete class name of the site list as well as a version identifier |
60 | * for the list's serialization, to avoid problems when unserializing site lists serialized |
61 | * by an older version, e.g. when reading from a cache. |
62 | * |
63 | * The cache key also includes information about where the sites were loaded from, e.g. |
64 | * the name of a database table. |
65 | * |
66 | * @see SiteList::getSerialVersionId |
67 | * |
68 | * @return string The cache key. |
69 | */ |
70 | private function getCacheKey() { |
71 | if ( $this->cacheKey === null ) { |
72 | $version = SiteList::getSerialVersionId(); |
73 | $this->cacheKey = $this->cache->makeKey( 'site-SiteList', $version ); |
74 | } |
75 | |
76 | return $this->cacheKey; |
77 | } |
78 | |
79 | /** |
80 | * @see SiteStore::getSites |
81 | * |
82 | * @since 1.25 |
83 | * @return SiteList |
84 | */ |
85 | public function getSites() { |
86 | if ( $this->sites === null ) { |
87 | $this->sites = $this->cache->getWithSetCallback( |
88 | $this->getCacheKey(), |
89 | BagOStuff::TTL_HOUR, |
90 | function () { |
91 | return $this->siteStore->getSites(); |
92 | } |
93 | ); |
94 | } |
95 | |
96 | return $this->sites; |
97 | } |
98 | |
99 | /** |
100 | * @see SiteStore::getSite |
101 | * |
102 | * @since 1.25 |
103 | * @param string $globalId |
104 | * @return Site|null |
105 | */ |
106 | public function getSite( $globalId ) { |
107 | $sites = $this->getSites(); |
108 | |
109 | return $sites->hasSite( $globalId ) ? $sites->getSite( $globalId ) : null; |
110 | } |
111 | |
112 | /** |
113 | * @see SiteStore::saveSite |
114 | * |
115 | * @since 1.25 |
116 | * @param Site $site |
117 | * @return bool Success indicator |
118 | */ |
119 | public function saveSite( Site $site ) { |
120 | return $this->saveSites( [ $site ] ); |
121 | } |
122 | |
123 | /** |
124 | * @see SiteStore::saveSites |
125 | * |
126 | * @since 1.25 |
127 | * @param Site[] $sites |
128 | * @return bool Success indicator |
129 | */ |
130 | public function saveSites( array $sites ) { |
131 | if ( !$sites ) { |
132 | return true; |
133 | } |
134 | |
135 | $success = $this->siteStore->saveSites( $sites ); |
136 | |
137 | // purge cache |
138 | $this->reset(); |
139 | |
140 | return $success; |
141 | } |
142 | |
143 | /** |
144 | * Purge the internal and external cache of the site list, forcing the list. |
145 | * of sites to be reloaded. |
146 | * |
147 | * @since 1.25 |
148 | */ |
149 | public function reset() { |
150 | $this->cache->delete( $this->getCacheKey() ); |
151 | $this->sites = null; |
152 | } |
153 | |
154 | /** |
155 | * Clears the list of sites stored. |
156 | * |
157 | * NOTE: The fact that this also clears the in-process cache is an internal |
158 | * detail for PHPUnit testing only. The injected cache is generally APCU, |
159 | * which is per-server, so the cache reset would not apply to any other web servers. |
160 | * |
161 | * @see SiteStore::clear() |
162 | * @return bool Success |
163 | */ |
164 | public function clear() { |
165 | $this->reset(); |
166 | |
167 | return $this->siteStore->clear(); |
168 | } |
169 | |
170 | } |
171 | |
172 | /** @deprecated class alias since 1.42 */ |
173 | class_alias( CachingSiteStore::class, 'CachingSiteStore' ); |