24use Wikimedia\ScopedCallback;
43 private $writesDone =
false;
45 private $readOnly =
false;
48 $this->server =
$params[
'server'] ?? [];
51 public function get( $code, $key ) {
52 if ( $this->server || $this->writesDone ) {
56 $db = $this->getWriteConnection();
58 $db = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
61 $value = $db->newSelectQueryBuilder()
62 ->select(
'lc_value' )
63 ->from(
'l10n_cache' )
64 ->where( [
'lc_lang' => $code,
'lc_key' => $key ] )
65 ->caller( __METHOD__ )->fetchField();
67 return ( $value !==
false ) ? unserialize( $db->decodeBlob( $value ) ) :
null;
71 if ( $this->readOnly ) {
74 throw new InvalidArgumentException( __METHOD__ .
": Invalid language \"$code\"" );
77 $dbw = $this->getWriteConnection();
85 if ( $this->readOnly ) {
87 } elseif ( $this->code ===
null ) {
88 throw new LogicException( __CLASS__ .
': must call startWrite() before finishWrite()' );
92 $dbw = $this->getWriteConnection();
96 ->deleteFrom(
'l10n_cache' )
97 ->where( [
'lc_lang' => $this->code ] )
98 ->caller( __METHOD__ )->execute();
99 foreach ( array_chunk( $this->batch, 500 ) as $rows ) {
101 ->insertInto(
'l10n_cache' )
103 ->caller( __METHOD__ )->execute();
105 $this->writesDone =
true;
108 $this->readOnly =
true;
114 ScopedCallback::consume( $scope );
120 public function set( $key, $value ) {
121 if ( $this->readOnly ) {
123 } elseif ( $this->code ===
null ) {
124 throw new LogicException( __CLASS__ .
': must call startWrite() before set()' );
127 $dbw = $this->getWriteConnection();
130 'lc_lang' => $this->code,
132 'lc_value' => $dbw->
encodeBlob( serialize( $value ) )
139 private function getWriteConnection() {
141 if ( $this->server ) {
142 $dbFactory = MediaWikiServices::getInstance()->getDatabaseFactory();
143 $this->dbw = $dbFactory->create( $this->server[
'type'], $this->server );
145 throw new RuntimeException( __CLASS__ .
': failed to obtain a DB connection' );
148 $this->dbw = MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase();
array $params
The job parameters.
LCStore implementation which uses the standard DB functions to store data.
finishWrite()
Finish a cache write transaction.
startWrite( $code)
Start a cache write transaction.
Interface for the persistence layer of LocalisationCache.