| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace Wikimedia\ObjectCache; |
| 22 |
|
| 23 |
use InvalidArgumentException; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class HashBagOStuff extends MediumSpecificBagOStuff { |
| 36 |
|
| 37 |
protected $bag = []; |
| 38 |
|
| 39 |
protected $maxCacheKeys; |
| 40 |
|
| 41 |
|
| 42 |
private $token; |
| 43 |
|
| 44 |
|
| 45 |
private static $casCounter = 0; |
| 46 |
|
| 47 |
public const KEY_VAL = 0; |
| 48 |
public const KEY_EXP = 1; |
| 49 |
public const KEY_CAS = 2; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public function __construct( $params = [] ) { |
| 61 |
$params['segmentationSize'] ??= INF; |
| 62 |
parent::__construct( $params ); |
| 63 |
|
| 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' ); |
| 68 |
} |
| 69 |
$this->maxCacheKeys = $maxKeys; |
| 70 |
|
| 71 |
$this->attrMap[self::ATTR_DURABILITY] = self::QOS_DURABILITY_SCRIPT; |
| 72 |
} |
| 73 |
|
| 74 |
protected function doGet( $key, $flags = 0, &$casToken = null ) { |
| 75 |
$getToken = ( $casToken === self::PASS_BY_REF ); |
| 76 |
$casToken = null; |
| 77 |
|
| 78 |
if ( !$this->hasKey( $key ) || $this->expire( $key ) ) { |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
$temp = $this->bag[$key]; |
| 84 |
unset( $this->bag[$key] ); |
| 85 |
$this->bag[$key] = $temp; |
| 86 |
|
| 87 |
$value = $this->bag[$key][self::KEY_VAL]; |
| 88 |
if ( $getToken && $value !== false ) { |
| 89 |
$casToken = $this->bag[$key][self::KEY_CAS]; |
| 90 |
} |
| 91 |
|
| 92 |
return $value; |
| 93 |
} |
| 94 |
|
| 95 |
protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) { |
| 96 |
|
| 97 |
unset( $this->bag[$key] ); |
| 98 |
$this->bag[$key] = [ |
| 99 |
self::KEY_VAL => $value, |
| 100 |
self::KEY_EXP => $this->getExpirationAsTimestamp( $exptime ), |
| 101 |
self::KEY_CAS => $this->token . ':' . ++self::$casCounter |
| 102 |
]; |
| 103 |
|
| 104 |
if ( count( $this->bag ) > $this->maxCacheKeys ) { |
| 105 |
$evictKey = array_key_first( $this->bag ); |
| 106 |
unset( $this->bag[$evictKey] ); |
| 107 |
} |
| 108 |
|
| 109 |
return true; |
| 110 |
} |
| 111 |
|
| 112 |
protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) { |
| 113 |
if ( $this->hasKey( $key ) && !$this->expire( $key ) ) { |
| 114 |
|
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
return $this->doSet( $key, $value, $exptime, $flags ); |
| 119 |
} |
| 120 |
|
| 121 |
protected function doDelete( $key, $flags = 0 ) { |
| 122 |
unset( $this->bag[$key] ); |
| 123 |
|
| 124 |
return true; |
| 125 |
} |
| 126 |
|
| 127 |
protected function doIncrWithInit( $key, $exptime, $step, $init, $flags ) { |
| 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 ); |
| 133 |
$this->bag[$key][self::KEY_VAL] = $newValue; |
| 134 |
} else { |
| 135 |
$newValue = false; |
| 136 |
} |
| 137 |
|
| 138 |
return $newValue; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public function clear() { |
| 145 |
$this->bag = []; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
protected function expire( $key ) { |
| 154 |
$et = $this->bag[$key][self::KEY_EXP]; |
| 155 |
if ( $et == self::TTL_INDEFINITE || $et > $this->getCurrentTime() ) { |
| 156 |
return false; |
| 157 |
} |
| 158 |
|
| 159 |
$this->doDelete( $key ); |
| 160 |
|
| 161 |
return true; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public function hasKey( $key ) { |
| 173 |
return isset( $this->bag[$key] ); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
class_alias( HashBagOStuff::class, 'HashBagOStuff' ); |