Move remaining LoadBalancer classes to Rdbms
[mediawiki.git] / includes / cache / localisation / LCStoreCDB.php
blob78a4863fe7ddb81279c11379f480d95c754f89df
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
20 use Cdb\Exception;
21 use Cdb\Reader;
22 use Cdb\Writer;
24 /**
25 * LCStore implementation which stores data as a collection of CDB files in the
26 * directory given by $wgCacheDirectory. If $wgCacheDirectory is not set, this
27 * will throw an exception.
29 * Profiling indicates that on Linux, this implementation outperforms MySQL if
30 * the directory is on a local filesystem and there is ample kernel cache
31 * space. The performance advantage is greater when the DBA extension is
32 * available than it is with the PHP port.
34 * See Cdb.php and https://cr.yp.to/cdb.html
36 class LCStoreCDB implements LCStore {
38 /** @var Reader[] */
39 private $readers;
41 /** @var Writer */
42 private $writer;
44 /** @var string Current language code */
45 private $currentLang;
47 /** @var bool|string Cache directory. False if not set */
48 private $directory;
50 function __construct( $conf = [] ) {
51 global $wgCacheDirectory;
53 if ( isset( $conf['directory'] ) ) {
54 $this->directory = $conf['directory'];
55 } else {
56 $this->directory = $wgCacheDirectory;
60 public function get( $code, $key ) {
61 if ( !isset( $this->readers[$code] ) ) {
62 $fileName = $this->getFileName( $code );
64 $this->readers[$code] = false;
65 if ( file_exists( $fileName ) ) {
66 try {
67 $this->readers[$code] = Reader::open( $fileName );
68 } catch ( Exception $e ) {
69 wfDebug( __METHOD__ . ": unable to open cdb file for reading\n" );
74 if ( !$this->readers[$code] ) {
75 return null;
76 } else {
77 $value = false;
78 try {
79 $value = $this->readers[$code]->get( $key );
80 } catch ( Exception $e ) {
81 wfDebug( __METHOD__ . ": \Cdb\Exception caught, error message was "
82 . $e->getMessage() . "\n" );
84 if ( $value === false ) {
85 return null;
88 return unserialize( $value );
92 public function startWrite( $code ) {
93 if ( !file_exists( $this->directory ) ) {
94 if ( !wfMkdirParents( $this->directory, null, __METHOD__ ) ) {
95 throw new MWException( "Unable to create the localisation store " .
96 "directory \"{$this->directory}\"" );
100 // Close reader to stop permission errors on write
101 if ( !empty( $this->readers[$code] ) ) {
102 $this->readers[$code]->close();
105 try {
106 $this->writer = Writer::open( $this->getFileName( $code ) );
107 } catch ( Exception $e ) {
108 throw new MWException( $e->getMessage() );
110 $this->currentLang = $code;
113 public function finishWrite() {
114 // Close the writer
115 try {
116 $this->writer->close();
117 } catch ( Exception $e ) {
118 throw new MWException( $e->getMessage() );
120 $this->writer = null;
121 unset( $this->readers[$this->currentLang] );
122 $this->currentLang = null;
125 public function set( $key, $value ) {
126 if ( is_null( $this->writer ) ) {
127 throw new MWException( __CLASS__ . ': must call startWrite() before calling set()' );
129 try {
130 $this->writer->set( $key, serialize( $value ) );
131 } catch ( Exception $e ) {
132 throw new MWException( $e->getMessage() );
136 protected function getFileName( $code ) {
137 if ( strval( $code ) === '' || strpos( $code, '/' ) !== false ) {
138 throw new MWException( __METHOD__ . ": Invalid language \"$code\"" );
141 return "{$this->directory}/l10n_cache-$code.cdb";