25use Psr\Log\LoggerInterface;
27use Wikimedia\Assert\Assert;
31use Wikimedia\RequestTimeout\TimeoutException;
97 $this->loadBalancer = $dbLoadBalancer;
104 $this->domain = $dbDomain;
105 $this->cacheTTL = IExpiringStore::TTL_MONTH;
116 return $this->loadBalancer->getConnectionRef( $index, [], $this->domain, $flags );
128 return $this->cache->makeGlobalKey(
131 $this->loadBalancer->resolveDomainID( $this->domain )
140 if ( $this->normalizationCallback ===
null ) {
143 return call_user_func( $this->normalizationCallback, $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 primary DB 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 () {
252 public function getId(
string $name ) {
256 $searchResult = array_search( $name,
$table,
true );
258 if ( $searchResult !==
false ) {
259 return $searchResult;
278 if ( array_key_exists( $id,
$table ) ) {
283 $table = $this->cache->getWithSetCallback(
286 function ( $oldValue, &$ttl, &$setOpts ) use ( $id, $fname ) {
288 if ( is_array( $oldValue ) && array_key_exists( $id, $oldValue ) ) {
290 $ttl = WANObjectCache::TTL_UNCACHEABLE;
299 $fname .
' falling back to primary select from ' .
300 $this->table .
' with id ' . $id
304 $cacheSetOpts = Database::getCacheSetOptions( $db );
306 if ( array_key_exists( $id,
$table ) ) {
311 $setOpts += $cacheSetOpts;
318 $this->tableCache =
$table;
320 if ( array_key_exists( $id,
$table ) ) {
342 if ( $this->tableCache !==
null ) {
346 $table = $this->cache->getWithSetCallback(
349 function ( $oldValue, &$ttl, &$setOpts ) {
351 $setOpts += Database::getCacheSetOptions(
$dbr );
356 $this->tableCache =
$table;
372 'id' => $this->idField,
373 'name' => $this->nameField
377 [
'ORDER BY' =>
'id' ]
381 foreach ( $result as $row ) {
382 $assocArray[$row->id] = $row->name;
394 private function store(
string $name ) {
395 Assert::parameter( $name !==
'',
'$name',
'should not be an empty string' );
401 $dbw->doAtomicSection(
404 use ( $name, &$id, $dbw ) {
415 if ( $dbw->affectedRows() === 0 ) {
417 'Tried to insert name into table ' . $this->table .
', but value already existed.'
423 $id = $dbw->insertId();
427 $dbw->onAtomicSectionCancel(
428 function ( $trigger,
IDatabase $unused ) use ( $name, $id, $dbw ) {
433 IDatabase::ATOMIC_CANCELABLE
456 function (
IDatabase $unused, $fname ) use ( $name, $id, $dbw ) {
469 function ( $trigger,
IDatabase $unused ) {
470 $this->logger->warning(
471 'Re-insertion of name into table ' . $this->table
472 .
' was rolled back. Giving up and reloading the cache.'
474 $this->
reloadMap( ILoadBalancer::CONN_TRX_AUTOCOMMIT );
480 'Re-insert name into table ' . $this->table .
' after failed transaction.'
483 IDatabase::ATOMIC_CANCELABLE
485 }
catch ( TimeoutException $e ) {
487 }
catch ( Exception $ex ) {
488 $this->logger->error(
489 'Re-insertion of name into table ' . $this->table .
' failed: ' . $ex->getMessage()
503 $this->
reloadMap( ILoadBalancer::CONN_TRX_AUTOCOMMIT );
517 if ( $id !==
null ) {
521 if ( $this->insertCallback !==
null ) {
522 $fields = call_user_func( $this->insertCallback, $fields );
Multi-datacenter aware caching interface.
Generic interface providing TTL constants for lightweight expiring object stores.