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 );
166 $searchResult = array_search( $name,
$table,
true );
167 if ( $searchResult ===
false ) {
168 $id = $this->
store( $name );
169 if ( $id ===
null ) {
174 $searchResult = array_search( $name,
$table,
true );
175 if ( $searchResult ===
false ) {
177 $m =
"No insert possible but primary DB didn't give us a record for " .
178 "'{$name}' in '{$this->table}'";
179 $this->logger->error( $m );
183 if ( isset(
$table[$id] ) ) {
190 $m =
"Got ID $id for '$name' from insert"
191 .
" into '{$this->table}', but ID $id was previously associated with"
192 .
" the name '{$table[$id]}'. Overriding the old value, which presumably"
193 .
" has been removed from the database due to a transaction rollback.";
195 $this->logger->warning( $m );
203 $dbw->onTransactionPreCommitOrIdle(
function () {
207 $this->tableCache =
$table;
210 return $searchResult;
226 if ( $connFlags !== 0 && defined(
'MW_PHPUNIT_TEST' ) ) {
233 $this->tableCache = $this->
loadTable( $dbw );
234 $dbw->onTransactionPreCommitOrIdle(
function () {
251 public function getId(
string $name ) {
255 $searchResult = array_search( $name,
$table,
true );
257 if ( $searchResult !==
false ) {
258 return $searchResult;
277 if ( array_key_exists( $id,
$table ) ) {
282 $table = $this->cache->getWithSetCallback(
285 function ( $oldValue, &$ttl, &$setOpts ) use ( $id, $fname ) {
287 if ( is_array( $oldValue ) && array_key_exists( $id, $oldValue ) ) {
289 $ttl = WANObjectCache::TTL_UNCACHEABLE;
298 $fname .
' falling back to primary select from ' .
299 $this->table .
' with id ' . $id
303 $cacheSetOpts = Database::getCacheSetOptions( $db );
305 if ( array_key_exists( $id,
$table ) ) {
310 $setOpts += $cacheSetOpts;
317 $this->tableCache =
$table;
319 if ( array_key_exists( $id,
$table ) ) {
341 if ( $this->tableCache !==
null ) {
345 $table = $this->cache->getWithSetCallback(
348 function ( $oldValue, &$ttl, &$setOpts ) {
350 $setOpts += Database::getCacheSetOptions(
$dbr );
355 $this->tableCache =
$table;
371 'id' => $this->idField,
372 'name' => $this->nameField
376 [
'ORDER BY' =>
'id' ]
380 foreach ( $result as $row ) {
381 $assocArray[$row->id] = $row->name;
393 private function store(
string $name ) {
394 Assert::parameter( $name !==
'',
'$name',
'should not be an empty string' );
400 $dbw->doAtomicSection(
403 use ( $name, &$id, $dbw ) {
414 if ( $dbw->affectedRows() === 0 ) {
416 'Tried to insert name into table ' . $this->table .
', but value already existed.'
422 $id = $dbw->insertId();
426 $dbw->onAtomicSectionCancel(
427 function ( $trigger,
IDatabase $unused ) use ( $name, $id, $dbw ) {
432 IDatabase::ATOMIC_CANCELABLE
455 function (
IDatabase $unused, $fname ) use ( $name, $id, $dbw ) {
468 function ( $trigger,
IDatabase $unused ) {
469 $this->logger->warning(
470 'Re-insertion of name into table ' . $this->table
471 .
' was rolled back. Giving up and reloading the cache.'
473 $this->
reloadMap( ILoadBalancer::CONN_TRX_AUTOCOMMIT );
479 'Re-insert name into table ' . $this->table .
' after failed transaction.'
482 IDatabase::ATOMIC_CANCELABLE
484 }
catch ( Exception $ex ) {
485 $this->logger->error(
486 'Re-insertion of name into table ' . $this->table .
' failed: ' . $ex->getMessage()
500 $this->
reloadMap( ILoadBalancer::CONN_TRX_AUTOCOMMIT );
514 if ( $id !==
null ) {
518 if ( $this->insertCallback !==
null ) {
519 $fields = call_user_func( $this->insertCallback, $fields );
Multi-datacenter aware caching interface.
Generic interface providing TTL constants for lightweight expiring object stores.