MediaWiki
master
Toggle main menu visibility
HashBagOStuff.php
Go to the documentation of this file.
1
<?php
6
7
namespace
Wikimedia\ObjectCache;
8
9
use InvalidArgumentException;
10
21
class
HashBagOStuff
extends
MediumSpecificBagOStuff
{
23
protected
$bag
= [];
25
protected
$maxCacheKeys
;
26
28
private
$token;
29
31
private
static
$casCounter = 0;
32
33
public
const
KEY_VAL
= 0;
34
public
const
KEY_EXP
= 1;
35
public
const
KEY_CAS
= 2;
36
46
public
function
__construct
( $params = [] ) {
47
$params[
'segmentationSize'
] ??= INF;
48
parent::__construct( $params );
49
50
$this->token = microtime(
true
) .
':'
. mt_rand();
51
$maxKeys = $params[
'maxKeys'
] ?? INF;
52
if
( $maxKeys !== INF && ( !is_int( $maxKeys ) || $maxKeys <= 0 ) ) {
53
throw
new
InvalidArgumentException(
'$maxKeys parameter must be above zero'
);
54
}
55
$this->maxCacheKeys = $maxKeys;
56
57
$this->attrMap[self::ATTR_DURABILITY] = self::QOS_DURABILITY_SCRIPT;
58
}
59
61
protected
function
doGet
( $key, $flags = 0, &$casToken =
null
) {
62
$getToken = ( $casToken === self::PASS_BY_REF );
63
$casToken =
null
;
64
65
if
( !$this->
hasKey
( $key ) || $this->
expire
( $key ) ) {
66
return
false
;
67
}
68
69
// Refresh key position for maxCacheKeys eviction
70
$temp = $this->bag[$key];
71
unset( $this->bag[$key] );
72
$this->bag[$key] = $temp;
73
74
$value = $this->bag[$key][self::KEY_VAL];
75
if
( $getToken && $value !==
false
) {
76
$casToken = $this->bag[$key][self::KEY_CAS];
77
}
78
79
return
$value;
80
}
81
83
protected
function
doSet
( $key, $value, $exptime = 0, $flags = 0 ) {
84
// Refresh key position for maxCacheKeys eviction
85
unset( $this->bag[$key] );
86
$this->bag[$key] = [
87
self::KEY_VAL => $value,
88
self::KEY_EXP => $this->
getExpirationAsTimestamp
( $exptime ),
89
self::KEY_CAS => $this->token .
':'
. ++self::$casCounter
90
];
91
92
if
( count( $this->bag ) > $this->maxCacheKeys ) {
93
$evictKey = array_key_first( $this->bag );
94
unset( $this->bag[$evictKey] );
95
}
96
97
return
true
;
98
}
99
101
protected
function
doAdd
( $key, $value, $exptime = 0, $flags = 0 ) {
102
if
( $this->
hasKey
( $key ) && !$this->
expire
( $key ) ) {
103
// key already set
104
return
false
;
105
}
106
107
return
$this->
doSet
( $key, $value, $exptime, $flags );
108
}
109
111
protected
function
doDelete
( $key, $flags = 0 ) {
112
unset( $this->bag[$key] );
113
114
return
true
;
115
}
116
118
protected
function
doIncrWithInit
( $key, $exptime, $step, $init, $flags ) {
119
$curValue = $this->
doGet
( $key );
120
if
( $curValue ===
false
) {
121
$newValue = $this->
doSet
( $key, $init, $exptime ) ? $init :
false
;
122
} elseif ( $this->
isInteger
( $curValue ) ) {
123
$newValue = max( $curValue + $step, 0 );
124
$this->bag[$key][self::KEY_VAL] = $newValue;
125
}
else
{
126
$newValue =
false
;
127
}
128
129
return
$newValue;
130
}
131
135
public
function
clear
() {
136
$this->bag = [];
137
}
138
144
protected
function
expire
( $key ) {
145
$et = $this->bag[$key][self::KEY_EXP];
146
if
( $et == self::TTL_INDEFINITE || $et > $this->
getCurrentTime
() ) {
147
return
false
;
148
}
149
150
$this->
doDelete
( $key );
151
152
return
true
;
153
}
154
163
public
function
hasKey
( $key ) {
164
return
isset( $this->bag[$key] );
165
}
166
}
167
169
class_alias( HashBagOStuff::class,
'HashBagOStuff'
);
Wikimedia\ObjectCache\BagOStuff\getCurrentTime
getCurrentTime()
Definition
BagOStuff.php:837
Wikimedia\ObjectCache\HashBagOStuff
Store data in a memory for the current request/process only.
Definition
HashBagOStuff.php:21
Wikimedia\ObjectCache\HashBagOStuff\__construct
__construct( $params=[])
Definition
HashBagOStuff.php:46
Wikimedia\ObjectCache\HashBagOStuff\KEY_VAL
const KEY_VAL
Definition
HashBagOStuff.php:33
Wikimedia\ObjectCache\HashBagOStuff\doSet
doSet( $key, $value, $exptime=0, $flags=0)
Set an item.bool Success
Definition
HashBagOStuff.php:83
Wikimedia\ObjectCache\HashBagOStuff\KEY_EXP
const KEY_EXP
Definition
HashBagOStuff.php:34
Wikimedia\ObjectCache\HashBagOStuff\KEY_CAS
const KEY_CAS
Definition
HashBagOStuff.php:35
Wikimedia\ObjectCache\HashBagOStuff\doGet
doGet( $key, $flags=0, &$casToken=null)
Get an item.The CAS token should be null if the key does not exist or the value is corruptmixed Retur...
Definition
HashBagOStuff.php:61
Wikimedia\ObjectCache\HashBagOStuff\doIncrWithInit
doIncrWithInit( $key, $exptime, $step, $init, $flags)
int|bool New value or false on failure
Definition
HashBagOStuff.php:118
Wikimedia\ObjectCache\HashBagOStuff\doDelete
doDelete( $key, $flags=0)
Delete an item.bool True if the item was deleted or not found, false on failure
Definition
HashBagOStuff.php:111
Wikimedia\ObjectCache\HashBagOStuff\hasKey
hasKey( $key)
Does this bag have a non-null value for the given key?
Definition
HashBagOStuff.php:163
Wikimedia\ObjectCache\HashBagOStuff\$maxCacheKeys
int double $maxCacheKeys
Max entries allowed, INF for unlimited.
Definition
HashBagOStuff.php:25
Wikimedia\ObjectCache\HashBagOStuff\$bag
mixed[] $bag
Definition
HashBagOStuff.php:23
Wikimedia\ObjectCache\HashBagOStuff\expire
expire( $key)
Definition
HashBagOStuff.php:144
Wikimedia\ObjectCache\HashBagOStuff\clear
clear()
Clear all values in cache.
Definition
HashBagOStuff.php:135
Wikimedia\ObjectCache\HashBagOStuff\doAdd
doAdd( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.bool Success
Definition
HashBagOStuff.php:101
Wikimedia\ObjectCache\MediumSpecificBagOStuff
Helper class that implements most of BagOStuff for a backend.
Definition
MediumSpecificBagOStuff.php:22
Wikimedia\ObjectCache\MediumSpecificBagOStuff\getExpirationAsTimestamp
getExpirationAsTimestamp( $exptime, $mtime=null)
Convert a potentially relative timestamp to an absolute timestamp.
Definition
MediumSpecificBagOStuff.php:939
Wikimedia\ObjectCache\MediumSpecificBagOStuff\isInteger
isInteger( $value)
Check if a value is an integer.
Definition
MediumSpecificBagOStuff.php:983
includes
libs
ObjectCache
HashBagOStuff.php
Generated on
for MediaWiki by
1.18.0