23use InvalidArgumentException;
45 private static $casCounter = 0;
61 $params[
'segmentationSize'] ??= INF;
64 $this->token = microtime(
true ) .
':' . mt_rand();
65 $maxKeys =
$params[
'maxKeys'] ?? INF;
66 if ( $maxKeys !== INF && ( !is_int( $maxKeys ) || $maxKeys <= 0 ) ) {
67 throw new InvalidArgumentException(
'$maxKeys parameter must be above zero' );
69 $this->maxCacheKeys = $maxKeys;
74 protected function doGet( $key, $flags = 0, &$casToken =
null ) {
83 $temp = $this->bag[$key];
84 unset( $this->bag[$key] );
85 $this->bag[$key] = $temp;
88 if ( $getToken && $value !==
false ) {
95 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
97 unset( $this->bag[$key] );
99 self::KEY_VAL => $value,
101 self::KEY_CAS => $this->token .
':' . ++self::$casCounter
104 if ( count( $this->bag ) > $this->maxCacheKeys ) {
105 $evictKey = array_key_first( $this->bag );
106 unset( $this->bag[$evictKey] );
112 protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
113 if ( $this->
hasKey( $key ) && !$this->
expire( $key ) ) {
118 return $this->
doSet( $key, $value, $exptime, $flags );
122 unset( $this->bag[$key] );
128 $curValue = $this->
doGet( $key );
129 if ( $curValue ===
false ) {
130 $newValue = $this->
doSet( $key, $init, $exptime ) ? $init :
false;
131 } elseif ( $this->
isInteger( $curValue ) ) {
132 $newValue = max( $curValue + $step, 0 );
155 if ( $et == self::TTL_INDEFINITE || $et > $this->
getCurrentTime() ) {
173 return isset( $this->bag[$key] );
178class_alias( HashBagOStuff::class,
'HashBagOStuff' );
array $params
The job parameters.