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  $maxKeys = $params['maxKeys'] ?? INF;
62  if ( $maxKeys !== INF && ( !is_int( $maxKeys ) || $maxKeys <= 0 ) ) {
63  throw new InvalidArgumentException( '$maxKeys parameter must be above zero' );
64  }
65  $this->maxCacheKeys = $maxKeys;
66 
67  $this->attrMap[self::ATTR_DURABILITY] = self::QOS_DURABILITY_SCRIPT;
68  $this->attrMap[self::ATTR_LOCALITY] = self::QOS_LOCALITY_PROC;
69  }
70 
71  protected function doGet( $key, $flags = 0, &$casToken = null ) {
72  $getToken = ( $casToken === self::PASS_BY_REF );
73  $casToken = null;
74 
75  if ( !$this->hasKey( $key ) || $this->expire( $key ) ) {
76  return false;
77  }
78 
79  // Refresh key position for maxCacheKeys eviction
80  $temp = $this->bag[$key];
81  unset( $this->bag[$key] );
82  $this->bag[$key] = $temp;
83 
84  if ( $getToken ) {
85  $casToken = $this->bag[$key][self::KEY_CAS];
86  }
87 
88  return $this->bag[$key][self::KEY_VAL];
89  }
90 
91  protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
92  // Refresh key position for maxCacheKeys eviction
93  unset( $this->bag[$key] );
94  $this->bag[$key] = [
95  self::KEY_VAL => $value,
96  self::KEY_EXP => $this->getExpirationAsTimestamp( $exptime ),
97  self::KEY_CAS => $this->token . ':' . ++self::$casCounter
98  ];
99 
100  if ( count( $this->bag ) > $this->maxCacheKeys ) {
101  reset( $this->bag );
102  $evictKey = key( $this->bag );
103  unset( $this->bag[$evictKey] );
104  }
105 
106  return true;
107  }
108 
109  protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
110  if ( $this->hasKey( $key ) && !$this->expire( $key ) ) {
111  return false; // key already set
112  }
113 
114  return $this->doSet( $key, $value, $exptime, $flags );
115  }
116 
117  protected function doDelete( $key, $flags = 0 ) {
118  unset( $this->bag[$key] );
119 
120  return true;
121  }
122 
123  public function incr( $key, $value = 1, $flags = 0 ) {
124  $n = $this->get( $key );
125  if ( $this->isInteger( $n ) ) {
126  $n = max( $n + (int)$value, 0 );
127  $this->bag[$key][self::KEY_VAL] = $n;
128 
129  return $n;
130  }
131 
132  return false;
133  }
134 
135  public function decr( $key, $value = 1, $flags = 0 ) {
136  return $this->incr( $key, -$value, $flags );
137  }
138 
142  public function clear() {
143  $this->bag = [];
144  }
145 
150  protected function expire( $key ) {
151  $et = $this->bag[$key][self::KEY_EXP];
152  if ( $et == self::TTL_INDEFINITE || $et > $this->getCurrentTime() ) {
153  return false;
154  }
155 
156  $this->doDelete( $key );
157 
158  return true;
159  }
160 
161  public function setNewPreparedValues( array $valueByKey ) {
162  // Do not bother with serialization as this class does not serialize values
163  $sizes = [];
164  foreach ( $valueByKey as $value ) {
165  $sizes[] = $this->guessSerialValueSize( $value );
166  }
167 
168  return $sizes;
169  }
170 
178  public function hasKey( $key ) {
179  return isset( $this->bag[$key] );
180  }
181 
182  public function makeKeyInternal( $keyspace, $components ) {
183  return $this->genericKeyFromComponents( $keyspace, ...$components );
184  }
185 
186  protected function convertGenericKey( $key ) {
187  return $key; // short-circuit; already uses "generic" keys
188  }
189 }
HashBagOStuff\KEY_EXP
const KEY_EXP
Definition: HashBagOStuff.php:45
MediumSpecificBagOStuff\isInteger
isInteger( $value)
Check if a value is an integer.
Definition: MediumSpecificBagOStuff.php:893
HashBagOStuff\$bag
mixed[] $bag
Definition: HashBagOStuff.php:34
HashBagOStuff\expire
expire( $key)
Definition: HashBagOStuff.php:150
HashBagOStuff\setNewPreparedValues
setNewPreparedValues(array $valueByKey)
Make a "generic" reversible cache key from the given components.
Definition: HashBagOStuff.php:161
MediumSpecificBagOStuff\guessSerialValueSize
guessSerialValueSize( $value, $depth=0, &$loops=0)
Estimate the size of a variable once serialized.
Definition: MediumSpecificBagOStuff.php:994
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:135
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:71
HashBagOStuff\KEY_CAS
const KEY_CAS
Definition: HashBagOStuff.php:46
HashBagOStuff\makeKeyInternal
makeKeyInternal( $keyspace, $components)
Make a cache key for the given keyspace and components.
Definition: HashBagOStuff.php:182
HashBagOStuff\hasKey
hasKey( $key)
Does this bag have a non-null value for the given key?
Definition: HashBagOStuff.php:178
BagOStuff\genericKeyFromComponents
genericKeyFromComponents(... $components)
At a minimum, there must be a keyspace and collection name component.
Definition: BagOStuff.php:685
HashBagOStuff\incr
incr( $key, $value=1, $flags=0)
Increase stored value of $key by $value while preserving its TTL.
Definition: HashBagOStuff.php:123
HashBagOStuff\clear
clear()
Clear all values in cache.
Definition: HashBagOStuff.php:142
MediumSpecificBagOStuff\getExpirationAsTimestamp
getExpirationAsTimestamp( $exptime)
Convert an optionally relative timestamp to an absolute time.
Definition: MediumSpecificBagOStuff.php:853
HashBagOStuff\convertGenericKey
convertGenericKey( $key)
Convert a "generic" reversible cache key into one for this cache.
Definition: HashBagOStuff.php:186
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\doSet
doSet( $key, $value, $exptime=0, $flags=0)
Set an item.
Definition: HashBagOStuff.php:91
HashBagOStuff\__construct
__construct( $params=[])
Definition: HashBagOStuff.php:56
HashBagOStuff\$maxCacheKeys
int double $maxCacheKeys
Max entries allowed, INF for unlimited.
Definition: HashBagOStuff.php:36
HashBagOStuff\doAdd
doAdd( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
Definition: HashBagOStuff.php:109
BagOStuff\getCurrentTime
getCurrentTime()
Definition: BagOStuff.php:804
BagOStuff\$keyspace
string $keyspace
Default keyspace; used by makeKey()
Definition: BagOStuff.php:103
HashBagOStuff\doDelete
doDelete( $key, $flags=0)
Delete an item.
Definition: HashBagOStuff.php:117