1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Wrapper around a BagOStuff that caches data in memory |
4
|
|
|
* |
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. |
9
|
|
|
* |
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. |
14
|
|
|
* |
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 |
19
|
|
|
* |
20
|
|
|
* @file |
21
|
|
|
* @ingroup Cache |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Wrapper around a BagOStuff that caches data in memory |
26
|
|
|
* |
27
|
|
|
* The differences between CachedBagOStuff and MultiWriteBagOStuff are: |
28
|
|
|
* * CachedBagOStuff supports only one "backend". |
29
|
|
|
* * There's a flag for writes to only go to the in-memory cache. |
30
|
|
|
* * The in-memory cache is always updated. |
31
|
|
|
* * Locks go to the backend cache (with MultiWriteBagOStuff, it would wind |
32
|
|
|
* up going to the HashBagOStuff used for the in-memory cache). |
33
|
|
|
* |
34
|
|
|
* @ingroup Cache |
35
|
|
|
*/ |
36
|
|
|
class CachedBagOStuff extends HashBagOStuff { |
37
|
|
|
/** @var BagOStuff */ |
38
|
|
|
protected $backend; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param BagOStuff $backend Permanent backend to use |
42
|
|
|
* @param array $params Parameters for HashBagOStuff |
43
|
|
|
*/ |
44
|
|
|
function __construct( BagOStuff $backend, $params = [] ) { |
45
|
|
|
unset( $params['reportDupes'] ); // useless here |
46
|
|
|
|
47
|
|
|
parent::__construct( $params ); |
48
|
|
|
|
49
|
|
|
$this->backend = $backend; |
50
|
|
|
$this->attrMap = $backend->attrMap; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function doGet( $key, $flags = 0 ) { |
54
|
|
|
$ret = parent::doGet( $key, $flags ); |
55
|
|
|
if ( $ret === false && !$this->hasKey( $key ) ) { |
56
|
|
|
$ret = $this->backend->doGet( $key, $flags ); |
57
|
|
|
$this->set( $key, $ret, 0, self::WRITE_CACHE_ONLY ); |
58
|
|
|
} |
59
|
|
|
return $ret; |
60
|
|
|
} |
61
|
|
|
|
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 ); |
66
|
|
|
} |
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function delete( $key, $flags = 0 ) { |
71
|
|
|
unset( $this->bag[$key] ); |
72
|
|
|
if ( !( $flags & self::WRITE_CACHE_ONLY ) ) { |
73
|
|
|
$this->backend->delete( $key ); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function setDebug( $bool ) { |
80
|
|
|
parent::setDebug( $bool ); |
81
|
|
|
$this->backend->setDebug( $bool ); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function lock( $key, $timeout = 6, $expiry = 6, $rclass = '' ) { |
85
|
|
|
return $this->backend->lock( $key, $timeout, $expiry, $rclass ); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function unlock( $key ) { |
89
|
|
|
return $this->backend->unlock( $key ); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) { |
93
|
|
|
parent::deleteObjectsExpiringBefore( $date, $progressCallback ); |
|
|
|
|
94
|
|
|
return $this->backend->deleteObjectsExpiringBefore( $date, $progressCallback ); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getLastError() { |
98
|
|
|
return $this->backend->getLastError(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function clearLastError() { |
102
|
|
|
$this->backend->clearLastError(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function modifySimpleRelayEvent( array $event ) { |
106
|
|
|
return $this->backend->modifySimpleRelayEvent( $event ); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: