wiki.techinc.nl/includes/parser/ParserCacheFactory.php
Petr Pchelko fec48eb5a4 Create ParserCacheFactory.
* Makes ParserCache take the root of the key
  as a constructor argument
* Introduces a ParserCacheFactory

Next steps:
- convert FlaggedRevs to using this.
- cleanup

This assumes that we wouldn't want to differentiate
the parser cache settings per use-case, as it is now
for default vs flaggedrevs caches. There are only two settings:
$wgParserCacheType - name of the BagOStuff to use
$wgParserCacheExpireTime - the expiration time.
I think if we wanted to have different settings for different
caches, we could add that as a next step.

Bug: T263583
Change-Id: I188772da541a95c95a5ecece7c7dd748395506c2
2020-09-25 18:17:58 -07:00

97 lines
2.5 KiB
PHP

<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup Cache Parser
*/
namespace MediaWiki\Parser;
use BagOStuff;
use IBufferingStatsdDataFactory;
use MediaWiki\HookContainer\HookContainer;
use ParserCache;
use Psr\Log\LoggerInterface;
/**
* Returns an instance of the ParserCache by its name.
* @since 1.36
* @package MediaWiki\Parser
*/
class ParserCacheFactory {
/** @var string name of ParserCache for the default parser */
public const DEFAULT_NAME = 'pcache';
/** @var BagOStuff */
private $cacheBackend;
/** @var string */
private $cacheEpoch;
/** @var HookContainer */
private $hookContainer;
/** @var IBufferingStatsdDataFactory */
private $stats;
/** @var LoggerInterface */
private $logger;
/** @var array */
private $instanceCache = [];
/**
* @param BagOStuff $cacheBackend
* @param string $cacheEpoch
* @param HookContainer $hookContainer
* @param IBufferingStatsdDataFactory $stats
* @param LoggerInterface $logger
*/
public function __construct(
BagOStuff $cacheBackend,
string $cacheEpoch,
HookContainer $hookContainer,
IBufferingStatsdDataFactory $stats,
LoggerInterface $logger
) {
$this->cacheBackend = $cacheBackend;
$this->cacheEpoch = $cacheEpoch;
$this->hookContainer = $hookContainer;
$this->stats = $stats;
$this->logger = $logger;
}
/**
* Get a ParserCache instance by $name.
* @param string $name
* @return ParserCache
*/
public function getInstance( string $name ) : ParserCache {
if ( !isset( $this->instanceCache[$name] ) ) {
$this->logger->debug( "Creating ParserCache instance for {$name}" );
$this->instanceCache[$name] = new ParserCache(
$this->cacheBackend,
$this->cacheEpoch,
$this->hookContainer,
$this->stats,
$name
);
}
return $this->instanceCache[$name];
}
}