MediaWiki  master
HashBagOStuff.php
Go to the documentation of this file.
1 <?php
34  protected $bag = [];
36  protected $maxCacheKeys;
37 
39  private $token;
40 
42  private static $casCounter = 0;
43 
44  public const KEY_VAL = 0;
45  public const KEY_EXP = 1;
46  public const KEY_CAS = 2;
47 
56  public function __construct( $params = [] ) {
57  $params['segmentationSize'] = $params['segmentationSize'] ?? INF;
58  parent::__construct( $params );
59 
60  $this->token = microtime( true ) . ':' . mt_rand();
61  $this->maxCacheKeys = $params['maxKeys'] ?? INF;
62  if ( $this->maxCacheKeys <= 0 ) {
63  throw new InvalidArgumentException( '$maxKeys parameter must be above zero' );
64  }
65 
66  $this->attrMap[self::ATTR_DURABILITY] = self::QOS_DURABILITY_SCRIPT;
67  $this->attrMap[self::ATTR_LOCALITY] = self::QOS_LOCALITY_PROC;
68  }
69 
70  protected function doGet( $key, $flags = 0, &$casToken = null ) {
71  $getToken = ( $casToken === self::PASS_BY_REF );
72  $casToken = null;
73 
74  if ( !$this->hasKey( $key ) || $this->expire( $key ) ) {
75  return false;
76  }
77 
78  // Refresh key position for maxCacheKeys eviction
79  $temp = $this->bag[$key];
80  unset( $this->bag[$key] );
81  $this->bag[$key] = $temp;
82 
83  if ( $getToken ) {
84  $casToken = $this->bag[$key][self::KEY_CAS];
85  }
86 
87  return $this->bag[$key][self::KEY_VAL];
88  }
89 
90  protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
91  // Refresh key position for maxCacheKeys eviction
92  unset( $this->bag[$key] );
93  $this->bag[$key] = [
94  self::KEY_VAL => $value,
95  self::KEY_EXP => $this->getExpirationAsTimestamp( $exptime ),
96  self::KEY_CAS => $this->token . ':' . ++self::$casCounter
97  ];
98 
99  if ( count( $this->bag ) > $this->maxCacheKeys ) {
100  reset( $this->bag );
101  $evictKey = key( $this->bag );
102  unset( $this->bag[$evictKey] );
103  }
104 
105  return true;
106  }
107 
108  protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
109  if ( $this->hasKey( $key ) && !$this->expire( $key ) ) {
110  return false; // key already set
111  }
112 
113  return $this->doSet( $key, $value, $exptime, $flags );
114  }
115 
116  protected function doDelete( $key, $flags = 0 ) {
117  unset( $this->bag[$key] );
118 
119  return true;
120  }
121 
122  public function incr( $key, $value = 1, $flags = 0 ) {
123  $n = $this->get( $key );
124  if ( $this->isInteger( $n ) ) {
125  $n = max( $n + (int)$value, 0 );
126  $this->bag[$key][self::KEY_VAL] = $n;
127 
128  return $n;
129  }
130 
131  return false;
132  }
133 
134  public function decr( $key, $value = 1, $flags = 0 ) {
135  return $this->incr( $key, -$value, $flags );
136  }
137 
141  public function clear() {
142  $this->bag = [];
143  }
144 
149  protected function expire( $key ) {
150  $et = $this->bag[$key][self::KEY_EXP];
151  if ( $et == self::TTL_INDEFINITE || $et > $this->getCurrentTime() ) {
152  return false;
153  }
154 
155  $this->doDelete( $key );
156 
157  return true;
158  }
159 
160  public function setNewPreparedValues( array $valueByKey ) {
161  // Do not bother with serialization as this class does not serialize values
162  $sizes = [];
163  foreach ( $valueByKey as $value ) {
164  $sizes[] = $this->guessSerialValueSize( $value );
165  }
166 
167  return $sizes;
168  }
169 
177  public function hasKey( $key ) {
178  return isset( $this->bag[$key] );
179  }
180 }
HashBagOStuff\KEY_EXP
const KEY_EXP
Definition: HashBagOStuff.php:45
MediumSpecificBagOStuff\isInteger
isInteger( $value)
Check if a value is an integer.
Definition: MediumSpecificBagOStuff.php:881
HashBagOStuff\$bag
mixed[] $bag
Definition: HashBagOStuff.php:34
HashBagOStuff\expire
expire( $key)
Definition: HashBagOStuff.php:149
HashBagOStuff\setNewPreparedValues
setNewPreparedValues(array $valueByKey)
Prepare values for storage and get their serialized sizes, or, estimate those sizes.
Definition: HashBagOStuff.php:160
MediumSpecificBagOStuff\guessSerialValueSize
guessSerialValueSize( $value, $depth=0, &$loops=0)
Estimate the size of a variable once serialized.
Definition: MediumSpecificBagOStuff.php:999
HashBagOStuff
Simple store for keeping values in an associative array for the current process.
Definition: HashBagOStuff.php:32
HashBagOStuff\decr
decr( $key, $value=1, $flags=0)
Decrease stored value of $key by $value while preserving its TTL.
Definition: HashBagOStuff.php:134
HashBagOStuff\$token
string $token
CAS token prefix for this instance.
Definition: HashBagOStuff.php:39
HashBagOStuff\doGet
doGet( $key, $flags=0, &$casToken=null)
Definition: HashBagOStuff.php:70
HashBagOStuff\KEY_CAS
const KEY_CAS
Definition: HashBagOStuff.php:46
HashBagOStuff\hasKey
hasKey( $key)
Does this bag have a non-null value for the given key?
Definition: HashBagOStuff.php:177
HashBagOStuff\incr
incr( $key, $value=1, $flags=0)
Increase stored value of $key by $value while preserving its TTL.
Definition: HashBagOStuff.php:122
HashBagOStuff\clear
clear()
Clear all values in cache.
Definition: HashBagOStuff.php:141
MediumSpecificBagOStuff\getExpirationAsTimestamp
getExpirationAsTimestamp( $exptime)
Convert an optionally relative timestamp to an absolute time.
Definition: MediumSpecificBagOStuff.php:841
MediumSpecificBagOStuff
Storage medium specific cache for storing items (e.g.
Definition: MediumSpecificBagOStuff.php:34
HashBagOStuff\KEY_VAL
const KEY_VAL
Definition: HashBagOStuff.php:44
HashBagOStuff\$casCounter
static int $casCounter
CAS token counter.
Definition: HashBagOStuff.php:42
HashBagOStuff\$maxCacheKeys
int $maxCacheKeys
Max entries allowed.
Definition: HashBagOStuff.php:36
HashBagOStuff\doSet
doSet( $key, $value, $exptime=0, $flags=0)
Set an item.
Definition: HashBagOStuff.php:90
HashBagOStuff\__construct
__construct( $params=[])
Stable to call.
Definition: HashBagOStuff.php:56
HashBagOStuff\doAdd
doAdd( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
Definition: HashBagOStuff.php:108
BagOStuff\getCurrentTime
getCurrentTime()
Definition: BagOStuff.php:567
HashBagOStuff\doDelete
doDelete( $key, $flags=0)
Delete an item.
Definition: HashBagOStuff.php:116