9use InvalidArgumentException;
31 private static $casCounter = 0;
47 $params[
'segmentationSize'] ??= INF;
48 parent::__construct( $params );
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' );
55 $this->maxCacheKeys = $maxKeys;
61 protected function doGet( $key, $flags = 0, &$casToken =
null ) {
70 $temp = $this->bag[$key];
71 unset( $this->bag[$key] );
72 $this->bag[$key] = $temp;
75 if ( $getToken && $value !==
false ) {
83 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
85 unset( $this->bag[$key] );
87 self::KEY_VAL => $value,
89 self::KEY_CAS => $this->token .
':' . ++self::$casCounter
92 if ( count( $this->bag ) > $this->maxCacheKeys ) {
93 $evictKey = array_key_first( $this->bag );
94 unset( $this->bag[$evictKey] );
101 protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
102 if ( $this->
hasKey( $key ) && !$this->
expire( $key ) ) {
107 return $this->
doSet( $key, $value, $exptime, $flags );
112 unset( $this->bag[$key] );
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 );
146 if ( $et == self::TTL_INDEFINITE || $et > $this->
getCurrentTime() ) {
164 return isset( $this->bag[$key] );
169class_alias( HashBagOStuff::class,
'HashBagOStuff' );