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 
67  protected function doGet( $key, $flags = 0, &$casToken = null ) {
68  $getToken = ( $casToken === self::PASS_BY_REF );
69  $casToken = null;
70 
71  if ( !$this->hasKey( $key ) || $this->expire( $key ) ) {
72  return false;
73  }
74 
75  // Refresh key position for maxCacheKeys eviction
76  $temp = $this->bag[$key];
77  unset( $this->bag[$key] );
78  $this->bag[$key] = $temp;
79 
80  if ( $getToken ) {
81  $casToken = $this->bag[$key][self::KEY_CAS];
82  }
83 
84  return $this->bag[$key][self::KEY_VAL];
85  }
86 
87  protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
88  // Refresh key position for maxCacheKeys eviction
89  unset( $this->bag[$key] );
90  $this->bag[$key] = [
91  self::KEY_VAL => $value,
92  self::KEY_EXP => $this->getExpirationAsTimestamp( $exptime ),
93  self::KEY_CAS => $this->token . ':' . ++self::$casCounter
94  ];
95 
96  if ( count( $this->bag ) > $this->maxCacheKeys ) {
97  reset( $this->bag );
98  $evictKey = key( $this->bag );
99  unset( $this->bag[$evictKey] );
100  }
101 
102  return true;
103  }
104 
105  protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
106  if ( $this->hasKey( $key ) && !$this->expire( $key ) ) {
107  return false; // key already set
108  }
109 
110  return $this->doSet( $key, $value, $exptime, $flags );
111  }
112 
113  protected function doDelete( $key, $flags = 0 ) {
114  unset( $this->bag[$key] );
115 
116  return true;
117  }
118 
119  public function incr( $key, $value = 1, $flags = 0 ) {
120  $n = $this->get( $key );
121  if ( $this->isInteger( $n ) ) {
122  $n = max( $n + (int)$value, 0 );
123  $this->bag[$key][self::KEY_VAL] = $n;
124 
125  return $n;
126  }
127 
128  return false;
129  }
130 
131  public function decr( $key, $value = 1, $flags = 0 ) {
132  return $this->incr( $key, -$value, $flags );
133  }
134 
138  public function clear() {
139  $this->bag = [];
140  }
141 
146  protected function expire( $key ) {
147  $et = $this->bag[$key][self::KEY_EXP];
148  if ( $et == self::TTL_INDEFINITE || $et > $this->getCurrentTime() ) {
149  return false;
150  }
151 
152  $this->doDelete( $key );
153 
154  return true;
155  }
156 
157  public function setNewPreparedValues( array $valueByKey ) {
158  // Do not bother with serialization as this class does not serialize values
159  $sizes = [];
160  foreach ( $valueByKey as $value ) {
161  $sizes[] = $this->guessSerialValueSize( $value );
162  }
163 
164  return $sizes;
165  }
166 
174  public function hasKey( $key ) {
175  return isset( $this->bag[$key] );
176  }
177 }
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:146
HashBagOStuff\setNewPreparedValues
setNewPreparedValues(array $valueByKey)
Prepare values for storage and get their serialized sizes, or, estimate those sizes.
Definition: HashBagOStuff.php:157
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:131
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:67
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:174
HashBagOStuff\incr
incr( $key, $value=1, $flags=0)
Increase stored value of $key by $value while preserving its TTL.
Definition: HashBagOStuff.php:119
HashBagOStuff\clear
clear()
Clear all values in cache.
Definition: HashBagOStuff.php:138
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:87
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:105
BagOStuff\getCurrentTime
getCurrentTime()
Definition: BagOStuff.php:566
HashBagOStuff\doDelete
doDelete( $key, $flags=0)
Delete an item.
Definition: HashBagOStuff.php:113