57 $params[
'segmentationSize'] = $params[
'segmentationSize'] ?? INF;
58 parent::__construct( $params );
60 $this->token = microtime(
true ) .
':' . mt_rand();
61 $maxKeys = $params[
'maxKeys'] ?? INF;
62 if ( $maxKeys !== INF && ( !is_int( $maxKeys ) || $maxKeys <= 0 ) ) {
63 throw new InvalidArgumentException(
'$maxKeys parameter must be above zero' );
65 $this->maxCacheKeys = $maxKeys;
68 protected function doGet( $key, $flags = 0, &$casToken =
null ) {
76 $temp = $this->bag[$key];
77 unset( $this->bag[$key] );
78 $this->bag[$key] = $temp;
80 $casToken = $this->bag[$key][self::KEY_CAS];
82 return $this->bag[$key][self::KEY_VAL];
85 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
87 unset( $this->bag[$key] );
89 self::KEY_VAL => $value,
91 self::KEY_CAS => $this->token .
':' . ++self::$casCounter
94 if ( count( $this->bag ) > $this->maxCacheKeys ) {
96 $evictKey = key( $this->bag );
97 unset( $this->bag[$evictKey] );
103 protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
104 if ( $this->
hasKey( $key ) && !$this->
expire( $key ) ) {
108 return $this->
doSet( $key, $value, $exptime, $flags );
112 unset( $this->bag[$key] );
117 public function incr( $key, $value = 1, $flags = 0 ) {
118 $n = $this->
get( $key );
120 $n = max( $n + (
int)$value, 0 );
121 $this->bag[$key][self::KEY_VAL] = $n;
129 public function decr( $key, $value = 1, $flags = 0 ) {
130 return $this->
incr( $key, -$value, $flags );
145 $et = $this->bag[$key][self::KEY_EXP];
146 if ( $et == self::TTL_INDEFINITE || $et > $this->
getCurrentTime() ) {
158 foreach ( $valueByKey as $value ) {
173 return isset( $this->bag[$key] );
Simple store for keeping values in an associative array for the current process.
int double $maxCacheKeys
Max entries allowed, INF for unlimited.
string $token
CAS token prefix for this instance.
setNewPreparedValues(array $valueByKey)
Prepare values for storage and get their serialized sizes, or, estimate those sizes.
clear()
Clear all values in cache.
decr( $key, $value=1, $flags=0)
Decrease stored value of $key by $value while preserving its TTL.
incr( $key, $value=1, $flags=0)
Increase stored value of $key by $value while preserving its TTL.
hasKey( $key)
Does this bag have a non-null value for the given key?
doAdd( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
doDelete( $key, $flags=0)
Delete an item.
__construct( $params=[])
Stable to call.
doGet( $key, $flags=0, &$casToken=null)
doSet( $key, $value, $exptime=0, $flags=0)
Set an item.
static int $casCounter
CAS token counter.
Storage medium specific cache for storing items (e.g.
guessSerialValueSize( $value, $depth=0, &$loops=0)
Estimate the size of a variable once serialized.
getExpirationAsTimestamp( $exptime)
Convert an optionally relative timestamp to an absolute time.
isInteger( $value)
Check if a value is an integer.