MediaWiki master
HashBagOStuff.php
Go to the documentation of this file.
1<?php
21namespace Wikimedia\ObjectCache;
22
23use InvalidArgumentException;
24
37 protected $bag = [];
39 protected $maxCacheKeys;
40
42 private $token;
43
45 private static $casCounter = 0;
46
47 public const KEY_VAL = 0;
48 public const KEY_EXP = 1;
49 public const KEY_CAS = 2;
50
60 public function __construct( $params = [] ) {
61 $params['segmentationSize'] ??= INF;
62 parent::__construct( $params );
63
64 $this->token = microtime( true ) . ':' . mt_rand();
65 $maxKeys = $params['maxKeys'] ?? INF;
66 if ( $maxKeys !== INF && ( !is_int( $maxKeys ) || $maxKeys <= 0 ) ) {
67 throw new InvalidArgumentException( '$maxKeys parameter must be above zero' );
68 }
69 $this->maxCacheKeys = $maxKeys;
70
72 }
73
74 protected function doGet( $key, $flags = 0, &$casToken = null ) {
75 $getToken = ( $casToken === self::PASS_BY_REF );
76 $casToken = null;
77
78 if ( !$this->hasKey( $key ) || $this->expire( $key ) ) {
79 return false;
80 }
81
82 // Refresh key position for maxCacheKeys eviction
83 $temp = $this->bag[$key];
84 unset( $this->bag[$key] );
85 $this->bag[$key] = $temp;
86
87 $value = $this->bag[$key][self::KEY_VAL];
88 if ( $getToken && $value !== false ) {
89 $casToken = $this->bag[$key][self::KEY_CAS];
90 }
91
92 return $value;
93 }
94
95 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
96 // Refresh key position for maxCacheKeys eviction
97 unset( $this->bag[$key] );
98 $this->bag[$key] = [
99 self::KEY_VAL => $value,
100 self::KEY_EXP => $this->getExpirationAsTimestamp( $exptime ),
101 self::KEY_CAS => $this->token . ':' . ++self::$casCounter
102 ];
103
104 if ( count( $this->bag ) > $this->maxCacheKeys ) {
105 $evictKey = array_key_first( $this->bag );
106 unset( $this->bag[$evictKey] );
107 }
108
109 return true;
110 }
111
112 protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
113 if ( $this->hasKey( $key ) && !$this->expire( $key ) ) {
114 // key already set
115 return false;
116 }
117
118 return $this->doSet( $key, $value, $exptime, $flags );
119 }
120
121 protected function doDelete( $key, $flags = 0 ) {
122 unset( $this->bag[$key] );
123
124 return true;
125 }
126
127 protected function doIncrWithInit( $key, $exptime, $step, $init, $flags ) {
128 $curValue = $this->doGet( $key );
129 if ( $curValue === false ) {
130 $newValue = $this->doSet( $key, $init, $exptime ) ? $init : false;
131 } elseif ( $this->isInteger( $curValue ) ) {
132 $newValue = max( $curValue + $step, 0 );
133 $this->bag[$key][self::KEY_VAL] = $newValue;
134 } else {
135 $newValue = false;
136 }
137
138 return $newValue;
139 }
140
144 public function clear() {
145 $this->bag = [];
146 }
147
153 protected function expire( $key ) {
154 $et = $this->bag[$key][self::KEY_EXP];
155 if ( $et == self::TTL_INDEFINITE || $et > $this->getCurrentTime() ) {
156 return false;
157 }
158
159 $this->doDelete( $key );
160
161 return true;
162 }
163
172 public function hasKey( $key ) {
173 return isset( $this->bag[$key] );
174 }
175}
176
178class_alias( HashBagOStuff::class, 'HashBagOStuff' );
array $params
The job parameters.
Store data in a memory for the current request/process only.
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.
Helper classs that implements most of BagOStuff for a backend.
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 ATTR_DURABILITY
Durability of writes; see QOS_DURABILITY_* (higher means stronger)
const QOS_DURABILITY_SCRIPT
Data is lost at the end of the current web request or CLI script.