MediaWiki REL1_35
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
68 protected function doGet( $key, $flags = 0, &$casToken = null ) {
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 $casToken = $this->bag[$key][self::KEY_CAS];
81
82 return $this->bag[$key][self::KEY_VAL];
83 }
84
85 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
86 // Refresh key position for maxCacheKeys eviction
87 unset( $this->bag[$key] );
88 $this->bag[$key] = [
89 self::KEY_VAL => $value,
90 self::KEY_EXP => $this->getExpirationAsTimestamp( $exptime ),
91 self::KEY_CAS => $this->token . ':' . ++self::$casCounter
92 ];
93
94 if ( count( $this->bag ) > $this->maxCacheKeys ) {
95 reset( $this->bag );
96 $evictKey = key( $this->bag );
97 unset( $this->bag[$evictKey] );
98 }
99
100 return true;
101 }
102
103 protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
104 if ( $this->hasKey( $key ) && !$this->expire( $key ) ) {
105 return false; // key already set
106 }
107
108 return $this->doSet( $key, $value, $exptime, $flags );
109 }
110
111 protected function doDelete( $key, $flags = 0 ) {
112 unset( $this->bag[$key] );
113
114 return true;
115 }
116
117 public function incr( $key, $value = 1, $flags = 0 ) {
118 $n = $this->get( $key );
119 if ( $this->isInteger( $n ) ) {
120 $n = max( $n + (int)$value, 0 );
121 $this->bag[$key][self::KEY_VAL] = $n;
122
123 return $n;
124 }
125
126 return false;
127 }
128
129 public function decr( $key, $value = 1, $flags = 0 ) {
130 return $this->incr( $key, -$value, $flags );
131 }
132
136 public function clear() {
137 $this->bag = [];
138 }
139
144 protected function expire( $key ) {
145 $et = $this->bag[$key][self::KEY_EXP];
146 if ( $et == self::TTL_INDEFINITE || $et > $this->getCurrentTime() ) {
147 return false;
148 }
149
150 $this->doDelete( $key );
151
152 return true;
153 }
154
155 public function setNewPreparedValues( array $valueByKey ) {
156 // Do not bother with serialization as this class does not serialize values
157 $sizes = [];
158 foreach ( $valueByKey as $value ) {
159 $sizes[] = $this->guessSerialValueSize( $value );
160 }
161
162 return $sizes;
163 }
164
172 public function hasKey( $key ) {
173 return isset( $this->bag[$key] );
174 }
175}
Simple store for keeping values in an associative array for the current process.
int double $maxCacheKeys
Max entries allowed, INF for unlimited.
string $token
CAS token prefix for this instance.
setNewPreparedValues(array $valueByKey)
Prepare values for storage and get their serialized sizes, or, estimate those sizes.
clear()
Clear all values in cache.
decr( $key, $value=1, $flags=0)
Decrease stored value of $key by $value while preserving its TTL.
incr( $key, $value=1, $flags=0)
Increase stored value of $key by $value while preserving its TTL.
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=[])
Stable to call.
doGet( $key, $flags=0, &$casToken=null)
doSet( $key, $value, $exptime=0, $flags=0)
Set an item.
static int $casCounter
CAS token counter.
Storage medium specific cache for storing items (e.g.
guessSerialValueSize( $value, $depth=0, &$loops=0)
Estimate the size of a variable once serialized.
getExpirationAsTimestamp( $exptime)
Convert an optionally relative timestamp to an absolute time.
isInteger( $value)
Check if a value is an integer.