PHPSessionHandler: Implement SessionHandlerInterface
[mediawiki.git] / includes / libs / objectcache / CachedBagOStuff.php
blobfc156187fefb053f39c74c69cf6f5c9a4eabde28
1 <?php
2 /**
3 * Wrapper around a BagOStuff that caches data in memory
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Cache
24 use Psr\Log\LoggerInterface;
26 /**
27 * Wrapper around a BagOStuff that caches data in memory
29 * The differences between CachedBagOStuff and MultiWriteBagOStuff are:
30 * * CachedBagOStuff supports only one "backend".
31 * * There's a flag for writes to only go to the in-memory cache.
32 * * The in-memory cache is always updated.
33 * * Locks go to the backend cache (with MultiWriteBagOStuff, it would wind
34 * up going to the HashBagOStuff used for the in-memory cache).
36 * @ingroup Cache
38 class CachedBagOStuff extends HashBagOStuff {
39 /** @var BagOStuff */
40 protected $backend;
42 /**
43 * @param BagOStuff $backend Permanent backend to use
44 * @param array $params Parameters for HashBagOStuff
46 function __construct( BagOStuff $backend, $params = array() ) {
47 $this->backend = $backend;
48 parent::__construct( $params );
51 protected function doGet( $key, $flags = 0 ) {
52 $ret = parent::doGet( $key, $flags );
53 if ( $ret === false ) {
54 $ret = $this->backend->doGet( $key, $flags );
55 if ( $ret !== false ) {
56 $this->set( $key, $ret, 0, self::WRITE_CACHE_ONLY );
59 return $ret;
62 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
63 parent::set( $key, $value, $exptime, $flags );
64 if ( !( $flags & self::WRITE_CACHE_ONLY ) ) {
65 $this->backend->set( $key, $value, $exptime, $flags & ~self::WRITE_CACHE_ONLY );
67 return true;
70 public function delete( $key, $flags = 0 ) {
71 unset( $this->bag[$key] );
72 if ( !( $flags & self::WRITE_CACHE_ONLY ) ) {
73 $this->backend->delete( $key );
76 return true;
79 public function setLogger( LoggerInterface $logger ) {
80 parent::setLogger( $logger );
81 $this->backend->setLogger( $logger );
84 public function setDebug( $bool ) {
85 parent::setDebug( $bool );
86 $this->backend->setDebug( $bool );
89 public function lock( $key, $timeout = 6, $expiry = 6, $rclass = '' ) {
90 return $this->backend->lock( $key, $timeout, $expiry, $rclass );
93 public function unlock( $key ) {
94 return $this->backend->unlock( $key );
97 public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
98 parent::deleteObjectsExpiringBefore( $date, $progressCallback );
99 return $this->backend->deleteObjectsExpiringBefore( $date, $progressCallback );
102 public function getLastError() {
103 return $this->backend->getLastError();
106 public function clearLastError() {
107 $this->backend->clearLastError();
110 public function modifySimpleRelayEvent( array $event ) {
111 return $this->backend->modifySimpleRelayEvent( $event );