MediaWiki  master
CachingSiteStore.php
Go to the documentation of this file.
1 <?php
2 
31 class CachingSiteStore implements SiteStore {
32 
36  private $sites = null;
37 
41  private $cacheKey;
42 
46  private $cacheTimeout;
47 
51  private $cache;
52 
56  private $siteStore;
57 
64  public function __construct(
67  $cacheKey = null,
68  $cacheTimeout = 3600
69  ) {
70  $this->siteStore = $siteStore;
71  $this->cache = $cache;
72  $this->cacheKey = $cacheKey;
73  $this->cacheTimeout = $cacheTimeout;
74  }
75 
90  private function getCacheKey() {
91  if ( $this->cacheKey === null ) {
92  $type = 'SiteList#' . SiteList::getSerialVersionId();
93  $this->cacheKey = $this->cache->makeKey( "sites/$type" );
94  }
95 
96  return $this->cacheKey;
97  }
98 
106  public function getSites() {
107  if ( $this->sites === null ) {
108  $this->sites = $this->cache->getWithSetCallback(
109  $this->getCacheKey(),
110  $this->cacheTimeout,
111  [ $this->siteStore, 'getSites' ]
112  );
113  }
114 
115  return $this->sites;
116  }
117 
127  public function getSite( $globalId ) {
128  $sites = $this->getSites();
129 
130  return $sites->hasSite( $globalId ) ? $sites->getSite( $globalId ) : null;
131  }
132 
142  public function saveSite( Site $site ) {
143  return $this->saveSites( [ $site ] );
144  }
145 
155  public function saveSites( array $sites ) {
156  if ( empty( $sites ) ) {
157  return true;
158  }
159 
160  $success = $this->siteStore->saveSites( $sites );
161 
162  // purge cache
163  $this->reset();
164 
165  return $success;
166  }
167 
176  public function reset() {
177  // purge cache
178  $this->cache->delete( $this->getCacheKey() );
179  $this->sites = null;
180  }
181 
191  public function clear() {
192  $this->reset();
193 
194  return $this->siteStore->clear();
195  }
196 
197 }
SiteList\getSerialVersionId
static getSerialVersionId()
Returns the version ID that identifies the serialization structure used by getSerializationData() and...
Definition: SiteList.php:310
CachingSiteStore\saveSite
saveSite(Site $site)
Definition: CachingSiteStore.php:142
CachingSiteStore
Definition: CachingSiteStore.php:31
CachingSiteStore\$cacheTimeout
int $cacheTimeout
Definition: CachingSiteStore.php:46
SiteList\hasSite
hasSite( $globalSiteId)
Returns if the list contains the site with the provided global site identifier.
Definition: SiteList.php:140
BagOStuff
Class representing a cache/ephemeral data store.
Definition: BagOStuff.php:86
$success
$success
Definition: NoLocalSettings.php:42
CachingSiteStore\clear
clear()
Clears the list of sites stored.
Definition: CachingSiteStore.php:191
CachingSiteStore\reset
reset()
Purges the internal and external cache of the site list, forcing the list.
Definition: CachingSiteStore.php:176
SiteStore
Definition: SiteStore.php:29
SiteList
Definition: SiteList.php:29
CachingSiteStore\getCacheKey
getCacheKey()
Constructs a cache key to use for caching the list of sites.
Definition: CachingSiteStore.php:90
CachingSiteStore\$cache
BagOStuff $cache
Definition: CachingSiteStore.php:51
CachingSiteStore\$sites
SiteList null $sites
Definition: CachingSiteStore.php:36
CachingSiteStore\saveSites
saveSites(array $sites)
Definition: CachingSiteStore.php:155
CachingSiteStore\$cacheKey
string null $cacheKey
Definition: CachingSiteStore.php:41
Site
Definition: Site.php:32
CachingSiteStore\$siteStore
SiteStore $siteStore
Definition: CachingSiteStore.php:56
CachingSiteStore\__construct
__construct(SiteStore $siteStore, BagOStuff $cache, $cacheKey=null, $cacheTimeout=3600)
Definition: CachingSiteStore.php:64
SiteList\getSite
getSite( $globalSiteId)
Returns the Site with the provided global site identifier.
Definition: SiteList.php:154
CachingSiteStore\getSites
getSites()
Definition: CachingSiteStore.php:106
CachingSiteStore\getSite
getSite( $globalId)
Definition: CachingSiteStore.php:127
$type
$type
Definition: testCompression.php:52