MediaWiki master
CachingSiteStore.php
Go to the documentation of this file.
1<?php
22
23use BagOStuff;
24
33class CachingSiteStore implements SiteStore {
35 private $siteStore;
37 private $cache;
38
40 private $cacheKey = null;
42 private $sites = null;
43
48 public function __construct(
49 SiteStore $siteStore,
50 BagOStuff $cache
51 ) {
52 $this->siteStore = $siteStore;
53 $this->cache = $cache;
54 }
55
70 private function getCacheKey() {
71 if ( $this->cacheKey === null ) {
73 $this->cacheKey = $this->cache->makeKey( 'site-SiteList', $version );
74 }
75
76 return $this->cacheKey;
77 }
78
85 public function getSites() {
86 if ( $this->sites === null ) {
87 $this->sites = $this->cache->getWithSetCallback(
88 $this->getCacheKey(),
89 BagOStuff::TTL_HOUR,
90 function () {
91 return $this->siteStore->getSites();
92 }
93 );
94 }
95
96 return $this->sites;
97 }
98
106 public function getSite( $globalId ) {
107 $sites = $this->getSites();
108
109 return $sites->hasSite( $globalId ) ? $sites->getSite( $globalId ) : null;
110 }
111
119 public function saveSite( Site $site ) {
120 return $this->saveSites( [ $site ] );
121 }
122
130 public function saveSites( array $sites ) {
131 if ( !$sites ) {
132 return true;
133 }
134
135 $success = $this->siteStore->saveSites( $sites );
136
137 // purge cache
138 $this->reset();
139
140 return $success;
141 }
142
149 public function reset() {
150 $this->cache->delete( $this->getCacheKey() );
151 $this->sites = null;
152 }
153
164 public function clear() {
165 $this->reset();
166
167 return $this->siteStore->clear();
168 }
169
170}
171
173class_alias( CachingSiteStore::class, 'CachingSiteStore' );
Class representing a cache/ephemeral data store.
Definition BagOStuff.php:85
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
Interface for storing and retrieving Site objects.
Definition SiteStore.php:32