Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
MessageGroupWANCache.php
Go to the documentation of this file.
1<?php
16
18 protected $cache;
24 protected $cacheKey;
30 protected $cacheVersion;
36 protected $regenerator;
41 protected $lockTSE;
46 protected $checkKeys;
51 protected $touchedCallback;
56 protected $ttl;
57
62 private const KEY_PREFIX = 'translate-mg';
63
64 public function __construct( WANObjectCache $cache ) {
65 $this->cache = $cache;
66 }
67
74 public function getValue( $recache = false ) {
75 $this->checkConfig();
76
77 $cacheData = $this->cache->getWithSetCallback(
78 $this->cacheKey,
79 $this->ttl,
80 $this->regenerator,
81 [
82 'lockTSE' => $this->lockTSE, // avoid stampedes (mutex)
83 'checkKeys' => $this->checkKeys,
84 'touchedCallback' => function ( $value ) {
85 if ( $this->touchedCallback && call_user_func( $this->touchedCallback, $value ) ) {
86 // treat value as if it just expired (for "lockTSE")
87 return time();
88 }
89
90 return null;
91 },
92 // "miss" on recache
93 'minAsOf' => $recache ? INF : WANObjectCache::MIN_TIMESTAMP_NONE,
94 ]
95 );
96
97 return $cacheData;
98 }
99
105 public function setValue( $cacheData ) {
106 $this->checkConfig();
107 $this->cache->set( $this->cacheKey, $cacheData, $this->ttl );
108 }
109
110 public function touchKey() {
111 $this->checkConfig();
112 $this->cache->touchCheckKey( $this->cacheKey );
113 }
114
118 public function delete() {
119 $this->checkConfig();
120 $this->cache->delete( $this->cacheKey );
121 }
122
129 public function configure( array $config ) {
130 $this->cacheKey = $config['key'] ?? null;
131 $this->cacheVersion = $config['version'] ?? null;
132 $this->regenerator = $config['regenerator'] ?? null;
133 $this->lockTSE = $config['lockTSE'] ?? 30;
134 $this->checkKeys = $config['checkKeys'] ?? [ $this->cacheKey ];
135 $this->touchedCallback = $config['touchedCallback'] ?? null;
136 $this->ttl = $config['ttl'] ?? WANObjectCache::TTL_DAY;
137
138 $this->checkConfig();
139
140 if ( $this->cacheVersion ) {
141 $this->cacheKey = $this->cache->makeKey( self::KEY_PREFIX,
142 strtolower( $this->cacheKey ), 'v' . $this->cacheVersion );
143 } else {
144 $this->cacheKey = $this->cache->makeKey(
145 self::KEY_PREFIX, strtolower( $this->cacheKey )
146 );
147 }
148 }
149
153 protected function checkConfig() {
154 if ( $this->cacheKey === null ) {
155 throw new InvalidArgumentException( "Invalid cache key set. " .
156 "Ensure you have called the configure function before get / setting values." );
157 }
158
159 if ( !is_callable( $this->regenerator ) ) {
160 throw new InvalidArgumentException( "Invalid regenerator set. " .
161 "Ensure you have called the configure function before get / setting values." );
162 }
163
164 if ( $this->touchedCallback && !is_callable( $this->touchedCallback ) ) {
165 throw new InvalidArgumentException( "touchedCallback is not callable. " );
166 }
167 }
168}
Wrapper around WANObjectCache providing a simpler interface for MessageGroups to use the cache.
configure(array $config)
Configure the message group.
checkConfig()
Check to see if the instance is configured properly.
getValue( $recache=false)
Fetches value from cache for a message group.
setValue( $cacheData)
Sets value in the cache for the message group.