Merge "docs: Fix typo"
[mediawiki.git] / includes / site / CachingSiteStore.php
blob398e72b77ac0b2521cb6f2088634d7e3b29230d8
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.
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.
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
18 * @file
21 namespace MediaWiki\Site;
23 use Wikimedia\ObjectCache\BagOStuff;
25 /**
26 * Hold a configured list of sites (SiteList), with a caching layer.
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 >
33 class CachingSiteStore implements SiteStore {
34 /** @var SiteStore */
35 private $siteStore;
36 /** @var BagOStuff */
37 private $cache;
39 /** @var string|null */
40 private $cacheKey = null;
41 /** @var SiteList|null */
42 private $sites = null;
44 public function __construct(
45 SiteStore $siteStore,
46 BagOStuff $cache
47 ) {
48 $this->siteStore = $siteStore;
49 $this->cache = $cache;
52 /**
53 * Constructs a cache key to use for caching the list of sites.
55 * This includes the concrete class name of the site list as well as a version identifier
56 * for the list's serialization, to avoid problems when unserializing site lists serialized
57 * by an older version, e.g. when reading from a cache.
59 * The cache key also includes information about where the sites were loaded from, e.g.
60 * the name of a database table.
62 * @see SiteList::getSerialVersionId
64 * @return string The cache key.
66 private function getCacheKey() {
67 if ( $this->cacheKey === null ) {
68 $version = SiteList::getSerialVersionId();
69 $this->cacheKey = $this->cache->makeKey( 'site-SiteList', $version );
72 return $this->cacheKey;
75 /**
76 * @see SiteStore::getSites
78 * @since 1.25
79 * @return SiteList
81 public function getSites() {
82 if ( $this->sites === null ) {
83 $this->sites = $this->cache->getWithSetCallback(
84 $this->getCacheKey(),
85 BagOStuff::TTL_HOUR,
86 function () {
87 return $this->siteStore->getSites();
92 return $this->sites;
95 /**
96 * @see SiteStore::getSite
98 * @since 1.25
99 * @param string $globalId
100 * @return Site|null
102 public function getSite( $globalId ) {
103 $sites = $this->getSites();
105 return $sites->hasSite( $globalId ) ? $sites->getSite( $globalId ) : null;
109 * @see SiteStore::saveSite
111 * @since 1.25
112 * @param Site $site
113 * @return bool Success indicator
115 public function saveSite( Site $site ) {
116 return $this->saveSites( [ $site ] );
120 * @see SiteStore::saveSites
122 * @since 1.25
123 * @param Site[] $sites
124 * @return bool Success indicator
126 public function saveSites( array $sites ) {
127 if ( !$sites ) {
128 return true;
131 $success = $this->siteStore->saveSites( $sites );
133 // purge cache
134 $this->reset();
136 return $success;
140 * Purge the internal and external cache of the site list, forcing the list.
141 * of sites to be reloaded.
143 * @since 1.25
145 public function reset() {
146 $this->cache->delete( $this->getCacheKey() );
147 $this->sites = null;
151 * Clears the list of sites stored.
153 * NOTE: The fact that this also clears the in-process cache is an internal
154 * detail for PHPUnit testing only. The injected cache is generally APCU,
155 * which is per-server, so the cache reset would not apply to any other web servers.
157 * @see SiteStore::clear()
158 * @return bool Success
160 public function clear() {
161 $this->reset();
163 return $this->siteStore->clear();
168 /** @deprecated class alias since 1.42 */
169 class_alias( CachingSiteStore::class, 'CachingSiteStore' );