55 $params[
'segmentationSize'] = $params[
'segmentationSize'] ?? INF;
56 parent::__construct( $params );
58 $this->token = microtime(
true ) .
':' . mt_rand();
59 $this->maxCacheKeys = $params[
'maxKeys'] ?? INF;
60 if ( $this->maxCacheKeys <= 0 ) {
61 throw new InvalidArgumentException(
'$maxKeys parameter must be above zero' );
65 protected function doGet( $key, $flags = 0, &$casToken =
null ) {
73 $temp = $this->bag[$key];
74 unset( $this->bag[$key] );
75 $this->bag[$key] = $temp;
77 $casToken = $this->bag[$key][self::KEY_CAS];
79 return $this->bag[$key][self::KEY_VAL];
82 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
84 unset( $this->bag[$key] );
86 self::KEY_VAL => $value,
88 self::KEY_CAS => $this->token .
':' . ++self::$casCounter
91 if ( count( $this->bag ) > $this->maxCacheKeys ) {
93 $evictKey = key( $this->bag );
94 unset( $this->bag[$evictKey] );
100 protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
101 if ( $this->
hasKey( $key ) && !$this->
expire( $key ) ) {
105 return $this->
doSet( $key, $value, $exptime, $flags );
109 unset( $this->bag[$key] );
114 public function incr( $key, $value = 1, $flags = 0 ) {
115 $n = $this->
get( $key );
117 $n = max( $n + (
int)$value, 0 );
118 $this->bag[$key][self::KEY_VAL] = $n;
126 public function decr( $key, $value = 1, $flags = 0 ) {
127 return $this->
incr( $key, -$value, $flags );
142 $et = $this->bag[$key][self::KEY_EXP];
143 if ( $et == self::TTL_INDEFINITE || $et > $this->
getCurrentTime() ) {
160 return isset( $this->bag[$key] );
Simple store for keeping values in an associative array for the current process.
string $token
CAS token prefix for this instance.
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.
doGet( $key, $flags=0, &$casToken=null)
doSet( $key, $value, $exptime=0, $flags=0)
Set an item.
static int $casCounter
CAS token counter.
int $maxCacheKeys
Max entries allowed.
Storage medium specific cache for storing items (e.g.
getExpirationAsTimestamp( $exptime)
Convert an optionally relative timestamp to an absolute time.
isInteger( $value)
Check if a value is an integer.