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
66 $this->attrMap[self::ATTR_DURABILITY] = self::QOS_DURABILITY_SCRIPT;
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 protected function doIncrWithInit( $key, $exptime, $step, $init, $flags ) {
123 $curValue = $this->doGet( $key );
124 if ( $curValue === false ) {
125 $newValue = $this->doSet( $key, $init, $exptime ) ? $init : false;
126 } elseif ( $this->isInteger( $curValue ) ) {
127 $newValue = max( $curValue + $step, 0 );
128 $this->bag[$key][self::KEY_VAL] = $newValue;
129 } else {
130 $newValue = false;
131 }
132
133 return $newValue;
134 }
135
139 public function clear() {
140 $this->bag = [];
141 }
142
147 protected function expire( $key ) {
148 $et = $this->bag[$key][self::KEY_EXP];
149 if ( $et == self::TTL_INDEFINITE || $et > $this->getCurrentTime() ) {
150 return false;
151 }
152
153 $this->doDelete( $key );
154
155 return true;
156 }
157
165 public function hasKey( $key ) {
166 return isset( $this->bag[$key] );
167 }
168}
array $params
The job parameters.
Simple store for keeping values in an associative array for the current process.
int double $maxCacheKeys
Max entries allowed, INF for unlimited.
clear()
Clear all values in cache.
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.
getExpirationAsTimestamp( $exptime)
Convert an optionally relative timestamp to an absolute time.
isInteger( $value)
Check if a value is an integer.