25use Psr\Log\LoggerInterface;
27use Wikimedia\Assert\Assert;
96 $this->loadBalancer = $dbLoadBalancer;
103 $this->domain = $dbDomain;
104 $this->cacheTTL = IExpiringStore::TTL_MONTH;
115 return $this->loadBalancer->getConnectionRef( $index, [], $this->domain, $flags );
127 return $this->cache->makeGlobalKey(
130 $this->loadBalancer->resolveDomainID( $this->domain )
139 if ( $this->normalizationCallback ===
null ) {
142 return call_user_func( $this->normalizationCallback, $name );
163 Assert::parameterType(
'string', $name,
'$name' );
167 $searchResult = array_search( $name,
$table,
true );
168 if ( $searchResult ===
false ) {
169 $id = $this->
store( $name );
170 if ( $id ===
null ) {
175 $searchResult = array_search( $name,
$table,
true );
176 if ( $searchResult ===
false ) {
178 $m =
"No insert possible but master didn't give us a record for " .
179 "'{$name}' in '{$this->table}'";
180 $this->logger->error( $m );
184 if ( isset(
$table[$id] ) ) {
191 $m =
"Got ID $id for '$name' from insert"
192 .
" into '{$this->table}', but ID $id was previously associated with"
193 .
" the name '{$table[$id]}'. Overriding the old value, which presumably"
194 .
" has been removed from the database due to a transaction rollback.";
196 $this->logger->warning( $m );
204 $dbw->onTransactionPreCommitOrIdle(
function () {
208 $this->tableCache =
$table;
211 return $searchResult;
227 if ( $connFlags !== 0 && defined(
'MW_PHPUNIT_TEST' ) ) {
234 $this->tableCache = $this->
loadTable( $dbw );
235 $dbw->onTransactionPreCommitOrIdle(
function () {
253 Assert::parameterType(
'string', $name,
'$name' );
257 $searchResult = array_search( $name,
$table,
true );
259 if ( $searchResult !==
false ) {
260 return $searchResult;
278 Assert::parameterType(
'integer', $id,
'$id' );
281 if ( array_key_exists( $id,
$table ) ) {
286 $table = $this->cache->getWithSetCallback(
289 function ( $oldValue, &$ttl, &$setOpts ) use ( $id, $fname ) {
291 if ( is_array( $oldValue ) && array_key_exists( $id, $oldValue ) ) {
293 $ttl = WANObjectCache::TTL_UNCACHEABLE;
302 $fname .
' falling back to master select from ' .
303 $this->table .
' with id ' . $id
307 $cacheSetOpts = Database::getCacheSetOptions( $db );
309 if ( array_key_exists( $id,
$table ) ) {
314 $setOpts += $cacheSetOpts;
321 $this->tableCache =
$table;
323 if ( array_key_exists( $id,
$table ) ) {
345 if ( $this->tableCache !==
null ) {
349 $table = $this->cache->getWithSetCallback(
352 function ( $oldValue, &$ttl, &$setOpts ) {
354 $setOpts += Database::getCacheSetOptions(
$dbr );
359 $this->tableCache =
$table;
375 'id' => $this->idField,
376 'name' => $this->nameField
380 [
'ORDER BY' =>
'id' ]
384 foreach ( $result as $row ) {
385 $assocArray[$row->id] = $row->name;
398 Assert::parameterType(
'string', $name,
'$name' );
399 Assert::parameter( $name !==
'',
'$name',
'should not be an empty string' );
405 $dbw->doAtomicSection(
408 use ( $name, &$id, $dbw ) {
419 if ( $dbw->affectedRows() === 0 ) {
421 'Tried to insert name into table ' . $this->table .
', but value already existed.'
427 $id = $dbw->insertId();
431 $dbw->onAtomicSectionCancel(
432 function ( $trigger,
IDatabase $unused ) use ( $name, $id, $dbw ) {
437 IDatabase::ATOMIC_CANCELABLE
460 function (
IDatabase $unused, $fname ) use ( $name, $id, &$ok, $dbw ) {
473 function ( $trigger,
IDatabase $unused ) use ( $name, $id, $dbw ) {
474 $this->logger->warning(
475 'Re-insertion of name into table ' . $this->table
476 .
' was rolled back. Giving up and reloading the cache.'
478 $this->
reloadMap( ILoadBalancer::CONN_TRX_AUTOCOMMIT );
484 'Re-insert name into table ' . $this->table .
' after failed transaction.'
487 IDatabase::ATOMIC_CANCELABLE
489 }
catch ( Exception $ex ) {
490 $this->logger->error(
491 'Re-insertion of name into table ' . $this->table .
' failed: ' . $ex->getMessage()
505 $this->
reloadMap( ILoadBalancer::CONN_TRX_AUTOCOMMIT );
519 if ( $id !==
null ) {
523 if ( $this->insertCallback !==
null ) {
524 $fields = call_user_func( $this->insertCallback, $fields );
Multi-datacenter aware caching interface.
Generic interface for lightweight expiring object stores.