MediaWiki master
CachingSiteStore.php
Go to the documentation of this file.
1<?php
22
24
33class CachingSiteStore implements SiteStore {
35 private $siteStore;
37 private $cache;
38
40 private $cacheKey = null;
42 private $sites = null;
43
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 ) {
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}
167
169class_alias( CachingSiteStore::class, 'CachingSiteStore' );
Hold a configured list of sites (SiteList), with a caching layer.
clear()
Clears the list of sites stored.
reset()
Purge the internal and external cache of the site list, forcing the list.
__construct(SiteStore $siteStore, BagOStuff $cache)
Array-like collection of Site objects.
Definition SiteList.php:37
static getSerialVersionId()
Returns the version ID that identifies the serialization structure used by __serialize() and unserial...
Definition SiteList.php:391
hasSite( $globalSiteId)
Returns if the list contains the site with the provided global site identifier.
Definition SiteList.php:222
getSite( $globalSiteId)
Returns the Site with the provided global site identifier.
Definition SiteList.php:236
Represents a single site.
Definition Site.php:36
Abstract class for any ephemeral data store.
Definition BagOStuff.php:88
Interface for storing and retrieving Site objects.
Definition SiteStore.php:32