10use Wikimedia\ScopedCallback;
29 private $writesDone =
false;
31 private $readOnly =
false;
34 $this->server = $params[
'server'] ?? [];
38 public function get( $code, $key ) {
39 if ( $this->server || $this->writesDone ) {
43 $db = $this->getWriteConnection();
45 $db = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
48 $value = $db->newSelectQueryBuilder()
49 ->select(
'lc_value' )
50 ->from(
'l10n_cache' )
51 ->where( [
'lc_lang' => $code,
'lc_key' => $key ] )
52 ->caller( __METHOD__ )->fetchField();
54 return ( $value !==
false ) ? unserialize( $db->decodeBlob( $value ) ) :
null;
59 if ( $this->readOnly ) {
62 throw new InvalidArgumentException( __METHOD__ .
": Invalid language \"$code\"" );
65 $dbw = $this->getWriteConnection();
73 if ( $this->readOnly ) {
75 } elseif ( $this->code ===
null ) {
76 throw new LogicException( __CLASS__ .
': must call startWrite() before finishWrite()' );
80 $dbw = $this->getWriteConnection();
84 ->deleteFrom(
'l10n_cache' )
85 ->where( [
'lc_lang' => $this->code ] )
86 ->caller( __METHOD__ )->execute();
87 foreach ( array_chunk( $this->batch, 500 ) as $rows ) {
89 ->insertInto(
'l10n_cache' )
91 ->caller( __METHOD__ )->execute();
93 $this->writesDone =
true;
96 $this->readOnly =
true;
102 ScopedCallback::consume( $scope );
109 public function set( $key, $value ) {
110 if ( $this->readOnly ) {
112 } elseif ( $this->code ===
null ) {
113 throw new LogicException( __CLASS__ .
': must call startWrite() before set()' );
116 $dbw = $this->getWriteConnection();
119 'lc_lang' => $this->code,
121 'lc_value' => $dbw->
encodeBlob( serialize( $value ) )
128 private function getWriteConnection() {
130 if ( $this->server ) {
131 $dbFactory = MediaWikiServices::getInstance()->getDatabaseFactory();
132 $this->dbw = $dbFactory->create( $this->server[
'type'], $this->server );
134 throw new RuntimeException( __CLASS__ .
': failed to obtain a DB connection' );
137 $this->dbw = MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase();
LCStore implementation which uses the standard DB functions to store data.
finishWrite()
Finish a cache write transaction.
startWrite( $code)
Start a cache write transaction.
__construct(array $params)
Interface for the persistence layer of LocalisationCache.