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 
55  public function __construct( $params = [] ) {
56  $params['segmentationSize'] ??= INF;
57  parent::__construct( $params );
58 
59  $this->token = microtime( true ) . ':' . mt_rand();
60  $maxKeys = $params['maxKeys'] ?? INF;
61  if ( $maxKeys !== INF && ( !is_int( $maxKeys ) || $maxKeys <= 0 ) ) {
62  throw new InvalidArgumentException( '$maxKeys parameter must be above zero' );
63  }
64  $this->maxCacheKeys = $maxKeys;
65 
67  }
68 
69  protected function doGet( $key, $flags = 0, &$casToken = null ) {
70  $getToken = ( $casToken === self::PASS_BY_REF );
71  $casToken = null;
72 
73  if ( !$this->hasKey( $key ) || $this->expire( $key ) ) {
74  return false;
75  }
76 
77  // Refresh key position for maxCacheKeys eviction
78  $temp = $this->bag[$key];
79  unset( $this->bag[$key] );
80  $this->bag[$key] = $temp;
81 
82  $value = $this->bag[$key][self::KEY_VAL];
83  if ( $getToken && $value !== false ) {
84  $casToken = $this->bag[$key][self::KEY_CAS];
85  }
86 
87  return $value;
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  $evictKey = array_key_first( $this->bag );
101  unset( $this->bag[$evictKey] );
102  }
103 
104  return true;
105  }
106 
107  protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
108  if ( $this->hasKey( $key ) && !$this->expire( $key ) ) {
109  // key already set
110  return false;
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  return $this->doIncr( $key, $value, $flags );
124  }
125 
126  public function decr( $key, $value = 1, $flags = 0 ) {
127  return $this->doIncr( $key, -$value, $flags );
128  }
129 
130  private function doIncr( $key, $value = 1, $flags = 0 ) {
131  $n = $this->doGet( $key );
132  if ( $this->isInteger( $n ) ) {
133  $n = max( $n + (int)$value, 0 );
134  $this->bag[$key][self::KEY_VAL] = $n;
135 
136  return $n;
137  }
138 
139  return false;
140  }
141 
142  protected function doIncrWithInit( $key, $exptime, $step, $init, $flags ) {
143  $curValue = $this->doGet( $key );
144  if ( $curValue === false ) {
145  $newValue = $this->doSet( $key, $init, $exptime ) ? $init : false;
146  } elseif ( $this->isInteger( $curValue ) ) {
147  $newValue = max( $curValue + $step, 0 );
148  $this->bag[$key][self::KEY_VAL] = $newValue;
149  } else {
150  $newValue = false;
151  }
152 
153  return $newValue;
154  }
155 
159  public function clear() {
160  $this->bag = [];
161  }
162 
167  protected function expire( $key ) {
168  $et = $this->bag[$key][self::KEY_EXP];
169  if ( $et == self::TTL_INDEFINITE || $et > $this->getCurrentTime() ) {
170  return false;
171  }
172 
173  $this->doDelete( $key );
174 
175  return true;
176  }
177 
185  public function hasKey( $key ) {
186  return isset( $this->bag[$key] );
187  }
188 
189  public function makeKeyInternal( $keyspace, $components ) {
190  return $this->genericKeyFromComponents( $keyspace, ...$components );
191  }
192 
193  protected function convertGenericKey( $key ) {
194  // short-circuit; already uses "generic" keys
195  return $key;
196  }
197 }
genericKeyFromComponents(... $components)
At a minimum, there must be a keyspace and collection name component.
Definition: BagOStuff.php:703
string $keyspace
Default keyspace; used by makeKey()
Definition: BagOStuff.php:101
getCurrentTime()
Definition: BagOStuff.php:834
Simple store for keeping values in an associative array for the current process.
int double $maxCacheKeys
Max entries allowed, INF for unlimited.
convertGenericKey( $key)
Convert a "generic" reversible cache key into one for this cache.
clear()
Clear all values in cache.
decr( $key, $value=1, $flags=0)
Decrease stored value of $key by $value while preserving its TTL.
makeKeyInternal( $keyspace, $components)
Make a cache key for the given keyspace and components.
incr( $key, $value=1, $flags=0)
Increase stored value of $key by $value while preserving its TTL.
doIncrWithInit( $key, $exptime, $step, $init, $flags)
hasKey( $key)
Does this bag have a non-null value for the given key?
doAdd( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
doDelete( $key, $flags=0)
Delete an item.
__construct( $params=[])
doGet( $key, $flags=0, &$casToken=null)
Get an item.
doSet( $key, $value, $exptime=0, $flags=0)
Set an item.
Storage medium specific cache for storing items (e.g.
const PASS_BY_REF
Idiom for doGet() to return extra information by reference.
getExpirationAsTimestamp( $exptime)
Convert an optionally relative timestamp to an absolute time.
isInteger( $value)
Check if a value is an integer.
const QOS_DURABILITY_SCRIPT
Data is lost at the end of the current web request or CLI script.
const ATTR_DURABILITY
Durability of writes; see QOS_DURABILITY_* (higher means stronger)