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
22 * A localisation cache optimised for loading large amounts of data for many
23 * languages. Used by rebuildLocalisationCache.php.
25 class LocalisationCacheBulkLoad
extends LocalisationCache
{
28 * A cache of the contents of data files.
29 * Core files are serialized to avoid using ~1GB of RAM during a recache.
31 private $fileCache = [];
34 * Most recently used languages. Uses the linked-list aspect of PHP hashtables
35 * to keep the most recently used language codes at the end of the array, and
36 * the language codes that are ready to be deleted at the beginning.
38 private $mruLangs = [];
41 * Maximum number of languages that may be loaded into $this->data
43 private $maxLoadedLangs = 10;
46 * @param string $fileName
47 * @param string $fileType
50 protected function readPHPFile( $fileName, $fileType ) {
51 $serialize = $fileType === 'core';
52 if ( !isset( $this->fileCache
[$fileName][$fileType] ) ) {
53 $data = parent
::readPHPFile( $fileName, $fileType );
56 $encData = serialize( $data );
61 $this->fileCache
[$fileName][$fileType] = $encData;
64 } elseif ( $serialize ) {
65 return unserialize( $this->fileCache
[$fileName][$fileType] );
67 return $this->fileCache
[$fileName][$fileType];
76 public function getItem( $code, $key ) {
77 unset( $this->mruLangs
[$code] );
78 $this->mruLangs
[$code] = true;
80 return parent
::getItem( $code, $key );
86 * @param string $subkey
89 public function getSubitem( $code, $key, $subkey ) {
90 unset( $this->mruLangs
[$code] );
91 $this->mruLangs
[$code] = true;
93 return parent
::getSubitem( $code, $key, $subkey );
99 public function recache( $code ) {
100 parent
::recache( $code );
101 unset( $this->mruLangs
[$code] );
102 $this->mruLangs
[$code] = true;
107 * @param string $code
109 public function unload( $code ) {
110 unset( $this->mruLangs
[$code] );
111 parent
::unload( $code );
115 * Unload cached languages until there are less than $this->maxLoadedLangs
117 protected function trimCache() {
118 while ( count( $this->data
) > $this->maxLoadedLangs
&& count( $this->mruLangs
) ) {
119 reset( $this->mruLangs
);
120 $code = key( $this->mruLangs
);
121 wfDebug( __METHOD__
. ": unloading $code\n" );
122 $this->unload( $code );