42 private static $casCounter = 0;
56 $params[
'segmentationSize'] ??= INF;
57 parent::__construct( $params );
59 $this->token = microtime(
true ) .
':' . mt_rand();
60 $maxKeys = $params[
'maxKeys'] ?? INF;
61 if ( $maxKeys !== INF && ( !is_int( $maxKeys ) || $maxKeys <= 0 ) ) {
62 throw new InvalidArgumentException(
'$maxKeys parameter must be above zero' );
64 $this->maxCacheKeys = $maxKeys;
66 $this->attrMap[self::ATTR_DURABILITY] = self::QOS_DURABILITY_SCRIPT;
69 protected function doGet( $key, $flags = 0, &$casToken =
null ) {
70 $getToken = ( $casToken === self::PASS_BY_REF );
78 $temp = $this->bag[$key];
79 unset( $this->bag[$key] );
80 $this->bag[$key] = $temp;
82 $value = $this->bag[$key][self::KEY_VAL];
83 if ( $getToken && $value !==
false ) {
84 $casToken = $this->bag[$key][self::KEY_CAS];
90 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
92 unset( $this->bag[$key] );
94 self::KEY_VAL => $value,
96 self::KEY_CAS => $this->token .
':' . ++self::$casCounter
99 if ( count( $this->bag ) > $this->maxCacheKeys ) {
100 $evictKey = array_key_first( $this->bag );
101 unset( $this->bag[$evictKey] );
107 protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
108 if ( $this->
hasKey( $key ) && !$this->
expire( $key ) ) {
113 return $this->
doSet( $key, $value, $exptime, $flags );
117 unset( $this->bag[$key] );
123 $curValue = $this->
doGet( $key );
124 if ( $curValue ===
false ) {
125 $newValue = $this->
doSet( $key, $init, $exptime ) ? $init :
false;
126 } elseif ( $this->
isInteger( $curValue ) ) {
127 $newValue = max( $curValue + $step, 0 );
128 $this->bag[$key][self::KEY_VAL] = $newValue;
148 $et = $this->bag[$key][self::KEY_EXP];
149 if ( $et == self::TTL_INDEFINITE || $et > $this->
getCurrentTime() ) {
166 return isset( $this->bag[$key] );
genericKeyFromComponents(... $components)
At a minimum, there must be a keyspace and collection name component.
string $keyspace
Default keyspace; used by makeKey()
Simple store for keeping values in an associative array for the current process.
int double $maxCacheKeys
Max entries allowed, INF for unlimited.
convertGenericKey( $key)
Convert a "generic" reversible cache key into one for this cache.
clear()
Clear all values in cache.
makeKeyInternal( $keyspace, $components)
Make a cache key for the given keyspace and components.
doIncrWithInit( $key, $exptime, $step, $init, $flags)
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)
Get an item.
doSet( $key, $value, $exptime=0, $flags=0)
Set an item.
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.