MediaWiki master
HashBagOStuff.php
Go to the documentation of this file.
1<?php
24namespace Wikimedia\ObjectCache;
25
26use InvalidArgumentException;
27
38 protected $bag = [];
40 protected $maxCacheKeys;
41
43 private $token;
44
46 private static $casCounter = 0;
47
48 public const KEY_VAL = 0;
49 public const KEY_EXP = 1;
50 public const KEY_CAS = 2;
51
61 public function __construct( $params = [] ) {
62 $params['segmentationSize'] ??= INF;
63 parent::__construct( $params );
64
65 $this->token = microtime( true ) . ':' . mt_rand();
66 $maxKeys = $params['maxKeys'] ?? INF;
67 if ( $maxKeys !== INF && ( !is_int( $maxKeys ) || $maxKeys <= 0 ) ) {
68 throw new InvalidArgumentException( '$maxKeys parameter must be above zero' );
69 }
70 $this->maxCacheKeys = $maxKeys;
71
73 }
74
75 protected function doGet( $key, $flags = 0, &$casToken = null ) {
76 $getToken = ( $casToken === self::PASS_BY_REF );
77 $casToken = null;
78
79 if ( !$this->hasKey( $key ) || $this->expire( $key ) ) {
80 return false;
81 }
82
83 // Refresh key position for maxCacheKeys eviction
84 $temp = $this->bag[$key];
85 unset( $this->bag[$key] );
86 $this->bag[$key] = $temp;
87
88 $value = $this->bag[$key][self::KEY_VAL];
89 if ( $getToken && $value !== false ) {
90 $casToken = $this->bag[$key][self::KEY_CAS];
91 }
92
93 return $value;
94 }
95
96 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
97 // Refresh key position for maxCacheKeys eviction
98 unset( $this->bag[$key] );
99 $this->bag[$key] = [
100 self::KEY_VAL => $value,
101 self::KEY_EXP => $this->getExpirationAsTimestamp( $exptime ),
102 self::KEY_CAS => $this->token . ':' . ++self::$casCounter
103 ];
104
105 if ( count( $this->bag ) > $this->maxCacheKeys ) {
106 $evictKey = array_key_first( $this->bag );
107 unset( $this->bag[$evictKey] );
108 }
109
110 return true;
111 }
112
113 protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
114 if ( $this->hasKey( $key ) && !$this->expire( $key ) ) {
115 // key already set
116 return false;
117 }
118
119 return $this->doSet( $key, $value, $exptime, $flags );
120 }
121
122 protected function doDelete( $key, $flags = 0 ) {
123 unset( $this->bag[$key] );
124
125 return true;
126 }
127
128 protected function doIncrWithInit( $key, $exptime, $step, $init, $flags ) {
129 $curValue = $this->doGet( $key );
130 if ( $curValue === false ) {
131 $newValue = $this->doSet( $key, $init, $exptime ) ? $init : false;
132 } elseif ( $this->isInteger( $curValue ) ) {
133 $newValue = max( $curValue + $step, 0 );
134 $this->bag[$key][self::KEY_VAL] = $newValue;
135 } else {
136 $newValue = false;
137 }
138
139 return $newValue;
140 }
141
145 public function clear() {
146 $this->bag = [];
147 }
148
154 protected function expire( $key ) {
155 $et = $this->bag[$key][self::KEY_EXP];
156 if ( $et == self::TTL_INDEFINITE || $et > $this->getCurrentTime() ) {
157 return false;
158 }
159
160 $this->doDelete( $key );
161
162 return true;
163 }
164
173 public function hasKey( $key ) {
174 return isset( $this->bag[$key] );
175 }
176}
177
179class_alias( HashBagOStuff::class, 'HashBagOStuff' );
array $params
The job parameters.
Simple store for keeping values in an associative array for the current process.
doSet( $key, $value, $exptime=0, $flags=0)
Set an item.
doGet( $key, $flags=0, &$casToken=null)
Get an item.
doIncrWithInit( $key, $exptime, $step, $init, $flags)
doDelete( $key, $flags=0)
Delete an item.
hasKey( $key)
Does this bag have a non-null value for the given key?
int double $maxCacheKeys
Max entries allowed, INF for unlimited.
clear()
Clear all values in cache.
doAdd( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
Storage medium specific cache for storing items (e.g.
isInteger( $value)
Check if a value is an integer.
const PASS_BY_REF
Idiom for doGet() to return extra information by reference.
getExpirationAsTimestamp( $exptime)
Convert an optionally relative timestamp to an absolute time.
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)