MediaWiki master
CachingSiteStore.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Site;
8
10
19class CachingSiteStore implements SiteStore {
21 private $siteStore;
23 private $cache;
25 private $cacheKey = null;
27 private $sites = null;
28
29 public function __construct(
30 SiteStore $siteStore,
31 BagOStuff $cache
32 ) {
33 $this->siteStore = $siteStore;
34 $this->cache = $cache;
35 }
36
51 private function getCacheKey() {
52 if ( $this->cacheKey === null ) {
54 $this->cacheKey = $this->cache->makeKey( 'site-SiteList', $version );
55 }
56
57 return $this->cacheKey;
58 }
59
66 public function getSites() {
67 if ( $this->sites === null ) {
68 $this->sites = $this->cache->getWithSetCallback(
69 $this->getCacheKey(),
70 BagOStuff::TTL_HOUR,
71 function () {
72 return $this->siteStore->getSites();
73 }
74 );
75 }
76
77 return $this->sites;
78 }
79
87 public function getSite( $globalId ) {
88 $sites = $this->getSites();
89
90 return $sites->hasSite( $globalId ) ? $sites->getSite( $globalId ) : null;
91 }
92
100 public function saveSite( Site $site ) {
101 return $this->saveSites( [ $site ] );
102 }
103
111 public function saveSites( array $sites ) {
112 if ( !$sites ) {
113 return true;
114 }
115
116 $success = $this->siteStore->saveSites( $sites );
117
118 // purge cache
119 $this->reset();
120
121 return $success;
122 }
123
130 public function reset() {
131 $this->cache->delete( $this->getCacheKey() );
132 $this->sites = null;
133 }
134
145 public function clear() {
146 $this->reset();
147
148 return $this->siteStore->clear();
149 }
150
151}
152
154class_alias( CachingSiteStore::class, 'CachingSiteStore' );
Wrap SiteList with an in-process cache and (optionally) a local-server cache.
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:23
static getSerialVersionId()
Returns the version ID that identifies the serialization structure used by __serialize() and unserial...
Definition SiteList.php:383
hasSite( $globalSiteId)
Returns if the list contains the site with the provided global site identifier.
Definition SiteList.php:214
getSite( $globalSiteId)
Returns the Site with the provided global site identifier.
Definition SiteList.php:228
Represents a single site.
Definition Site.php:22
Abstract class for any ephemeral data store.
Definition BagOStuff.php:73
Interface for storing and retrieving Site objects.
Definition SiteStore.php:18