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
21 namespace Wikimedia\ObjectCache
;
23 use InvalidArgumentException
;
26 * Store data in a memory for the current request/process only.
28 * This keeps values in a simple associative array.
29 * Data will not persist and is not shared with other requests
35 class HashBagOStuff
extends MediumSpecificBagOStuff
{
38 /** @var int|double Max entries allowed, INF for unlimited */
39 protected $maxCacheKeys;
41 /** @var string CAS token prefix for this instance */
44 /** @var int CAS token counter */
45 private static $casCounter = 0;
47 public const KEY_VAL
= 0;
48 public const KEY_EXP
= 1;
49 public const KEY_CAS
= 2;
54 * @param array $params Additional parameters include:
55 * - maxKeys : only allow this many keys (using oldest-first eviction)
57 * @phpcs:ignore Generic.Files.LineLength
58 * @phan-param array{logger?:\Psr\Log\LoggerInterface,asyncHandler?:callable,keyspace?:string,reportDupes?:bool,segmentationSize?:int,segmentedValueMaxSize?:int,maxKeys?:int} $params
60 public function __construct( $params = [] ) {
61 $params['segmentationSize'] ??
= INF
;
62 parent
::__construct( $params );
64 $this->token
= microtime( true ) . ':' . mt_rand();
65 $maxKeys = $params['maxKeys'] ?? INF
;
66 if ( $maxKeys !== INF
&& ( !is_int( $maxKeys ) ||
$maxKeys <= 0 ) ) {
67 throw new InvalidArgumentException( '$maxKeys parameter must be above zero' );
69 $this->maxCacheKeys
= $maxKeys;
71 $this->attrMap
[self
::ATTR_DURABILITY
] = self
::QOS_DURABILITY_SCRIPT
;
74 protected function doGet( $key, $flags = 0, &$casToken = null ) {
75 $getToken = ( $casToken === self
::PASS_BY_REF
);
78 if ( !$this->hasKey( $key ) ||
$this->expire( $key ) ) {
82 // Refresh key position for maxCacheKeys eviction
83 $temp = $this->bag
[$key];
84 unset( $this->bag
[$key] );
85 $this->bag
[$key] = $temp;
87 $value = $this->bag
[$key][self
::KEY_VAL
];
88 if ( $getToken && $value !== false ) {
89 $casToken = $this->bag
[$key][self
::KEY_CAS
];
95 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
96 // Refresh key position for maxCacheKeys eviction
97 unset( $this->bag
[$key] );
99 self
::KEY_VAL
=> $value,
100 self
::KEY_EXP
=> $this->getExpirationAsTimestamp( $exptime ),
101 self
::KEY_CAS
=> $this->token
. ':' . ++self
::$casCounter
104 if ( count( $this->bag
) > $this->maxCacheKeys
) {
105 $evictKey = array_key_first( $this->bag
);
106 unset( $this->bag
[$evictKey] );
112 protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
113 if ( $this->hasKey( $key ) && !$this->expire( $key ) ) {
118 return $this->doSet( $key, $value, $exptime, $flags );
121 protected function doDelete( $key, $flags = 0 ) {
122 unset( $this->bag
[$key] );
127 protected function doIncrWithInit( $key, $exptime, $step, $init, $flags ) {
128 $curValue = $this->doGet( $key );
129 if ( $curValue === false ) {
130 $newValue = $this->doSet( $key, $init, $exptime ) ?
$init : false;
131 } elseif ( $this->isInteger( $curValue ) ) {
132 $newValue = max( $curValue +
$step, 0 );
133 $this->bag
[$key][self
::KEY_VAL
] = $newValue;
142 * Clear all values in cache
144 public function clear() {
153 protected function expire( $key ) {
154 $et = $this->bag
[$key][self
::KEY_EXP
];
155 if ( $et == self
::TTL_INDEFINITE ||
$et > $this->getCurrentTime() ) {
159 $this->doDelete( $key );
165 * Does this bag have a non-null value for the given key?
172 public function hasKey( $key ) {
173 return isset( $this->bag
[$key] );
177 /** @deprecated class alias since 1.43 */
178 class_alias( HashBagOStuff
::class, 'HashBagOStuff' );