4 * Represents the site configuration of a wiki.
5 * Holds a list of sites (ie SiteList), with a caching layer.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
27 * @license GNU GPL v2+
28 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
29 * @author Katie Filbert < aude.wiki@gmail.com >
31 class CachingSiteStore
implements SiteStore
{
36 private $sites = null;
46 private $cacheTimeout;
59 * @param SiteStore $siteStore
60 * @param BagOStuff $cache
61 * @param string|null $cacheKey
62 * @param int $cacheTimeout
64 public function __construct(
70 $this->siteStore
= $siteStore;
71 $this->cache
= $cache;
72 $this->cacheKey
= $cacheKey;
73 $this->cacheTimeout
= $cacheTimeout;
77 * Constructs a cache key to use for caching the list of sites.
79 * This includes the concrete class name of the site list as well as a version identifier
80 * for the list's serialization, to avoid problems when unserializing site lists serialized
81 * by an older version, e.g. when reading from a cache.
83 * The cache key also includes information about where the sites were loaded from, e.g.
84 * the name of a database table.
86 * @see SiteList::getSerialVersionId
88 * @return string The cache key.
90 private function getCacheKey() {
91 if ( $this->cacheKey
=== null ) {
92 $type = 'SiteList#' . SiteList
::getSerialVersionId();
93 $this->cacheKey
= $this->cache
->makeKey( "sites/$type" );
96 return $this->cacheKey
;
100 * @see SiteStore::getSites
106 public function getSites() {
107 if ( $this->sites
=== null ) {
108 $this->sites
= $this->cache
->get( $this->getCacheKey() );
110 if ( !is_object( $this->sites
) ) {
111 $this->sites
= $this->siteStore
->getSites();
113 $this->cache
->set( $this->getCacheKey(), $this->sites
, $this->cacheTimeout
);
121 * @see SiteStore::getSite
125 * @param string $globalId
129 public function getSite( $globalId ) {
130 $sites = $this->getSites();
132 return $sites->hasSite( $globalId ) ?
$sites->getSite( $globalId ) : null;
136 * @see SiteStore::saveSite
142 * @return bool Success indicator
144 public function saveSite( Site
$site ) {
145 return $this->saveSites( [ $site ] );
149 * @see SiteStore::saveSites
153 * @param Site[] $sites
155 * @return bool Success indicator
157 public function saveSites( array $sites ) {
158 if ( empty( $sites ) ) {
162 $success = $this->siteStore
->saveSites( $sites );
171 * Purges the internal and external cache of the site list, forcing the list.
172 * of sites to be reloaded.
174 * Only use this for testing, as APC is typically used and is per-server
178 public function reset() {
180 $this->cache
->delete( $this->getCacheKey() );
185 * Clears the list of sites stored.
187 * Only use this for testing, as APC is typically used and is per-server.
189 * @see SiteStore::clear()
191 * @return bool Success
193 public function clear() {
196 return $this->siteStore
->clear();