MediaWiki  master
CachingSiteStore.php
Go to the documentation of this file.
1 <?php
29 class CachingSiteStore implements SiteStore {
31  private $siteStore;
33  private $cache;
34 
36  private $cacheKey = null;
38  private $sites = null;
39 
44  public function __construct(
45  SiteStore $siteStore,
46  BagOStuff $cache
47  ) {
48  $this->siteStore = $siteStore;
49  $this->cache = $cache;
50  }
51 
66  private function getCacheKey() {
67  if ( $this->cacheKey === null ) {
68  $version = SiteList::getSerialVersionId();
69  $this->cacheKey = $this->cache->makeKey( 'site-SiteList', $version );
70  }
71 
72  return $this->cacheKey;
73  }
74 
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();
88  }
89  );
90  }
91 
92  return $this->sites;
93  }
94 
102  public function getSite( $globalId ) {
103  $sites = $this->getSites();
104 
105  return $sites->hasSite( $globalId ) ? $sites->getSite( $globalId ) : null;
106  }
107 
115  public function saveSite( Site $site ) {
116  return $this->saveSites( [ $site ] );
117  }
118 
126  public function saveSites( array $sites ) {
127  if ( !$sites ) {
128  return true;
129  }
130 
131  $success = $this->siteStore->saveSites( $sites );
132 
133  // purge cache
134  $this->reset();
135 
136  return $success;
137  }
138 
145  public function reset() {
146  $this->cache->delete( $this->getCacheKey() );
147  $this->sites = null;
148  }
149 
160  public function clear() {
161  $this->reset();
162 
163  return $this->siteStore->clear();
164  }
165 
166 }
$success
Class representing a cache/ephemeral data store.
Definition: BagOStuff.php:85
Hold a configured list of sites (SiteList), with a caching layer.
saveSites(array $sites)
__construct(SiteStore $siteStore, BagOStuff $cache)
clear()
Clears the list of sites stored.
reset()
Purge the internal and external cache of the site list, forcing the list.
static getSerialVersionId()
Returns the version ID that identifies the serialization structure used by __serialize() and unserial...
Definition: SiteList.php:387
hasSite( $globalSiteId)
Returns if the list contains the site with the provided global site identifier.
Definition: SiteList.php:218
getSite( $globalSiteId)
Returns the Site with the provided global site identifier.
Definition: SiteList.php:232
Represents a single site.
Definition: Site.php:32
Interface for storing and retrieving Site objects.
Definition: SiteStore.php:30