MediaWiki master
HashBagOStuff.php
Go to the documentation of this file.
1<?php
8
9use InvalidArgumentException;
10
23 protected $bag = [];
25 protected $maxCacheKeys;
26
28 private $token;
29
31 private static $casCounter = 0;
32
33 public const KEY_VAL = 0;
34 public const KEY_EXP = 1;
35 public const KEY_CAS = 2;
36
46 public function __construct( $params = [] ) {
47 $params['segmentationSize'] ??= INF;
48 parent::__construct( $params );
49
50 $this->token = microtime( true ) . ':' . mt_rand();
51 $maxKeys = $params['maxKeys'] ?? INF;
52 if ( $maxKeys !== INF && ( !is_int( $maxKeys ) || $maxKeys <= 0 ) ) {
53 throw new InvalidArgumentException( '$maxKeys parameter must be above zero' );
54 }
55 $this->maxCacheKeys = $maxKeys;
56
58 }
59
61 protected function doGet( $key, $flags = 0, &$casToken = null ) {
62 $getToken = ( $casToken === self::PASS_BY_REF );
63 $casToken = null;
64
65 if ( !$this->hasKey( $key ) || $this->expire( $key ) ) {
66 return false;
67 }
68
69 // Refresh key position for maxCacheKeys eviction
70 $temp = $this->bag[$key];
71 unset( $this->bag[$key] );
72 $this->bag[$key] = $temp;
73
74 $value = $this->bag[$key][self::KEY_VAL];
75 if ( $getToken && $value !== false ) {
76 $casToken = $this->bag[$key][self::KEY_CAS];
77 }
78
79 return $value;
80 }
81
83 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
84 // Refresh key position for maxCacheKeys eviction
85 unset( $this->bag[$key] );
86 $this->bag[$key] = [
87 self::KEY_VAL => $value,
88 self::KEY_EXP => $this->getExpirationAsTimestamp( $exptime ),
89 self::KEY_CAS => $this->token . ':' . ++self::$casCounter
90 ];
91
92 if ( count( $this->bag ) > $this->maxCacheKeys ) {
93 $evictKey = array_key_first( $this->bag );
94 unset( $this->bag[$evictKey] );
95 }
96
97 return true;
98 }
99
101 protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
102 if ( $this->hasKey( $key ) && !$this->expire( $key ) ) {
103 // key already set
104 return false;
105 }
106
107 return $this->doSet( $key, $value, $exptime, $flags );
108 }
109
111 protected function doDelete( $key, $flags = 0 ) {
112 unset( $this->bag[$key] );
113
114 return true;
115 }
116
118 protected function doIncrWithInit( $key, $exptime, $step, $init, $flags ) {
119 $curValue = $this->doGet( $key );
120 if ( $curValue === false ) {
121 $newValue = $this->doSet( $key, $init, $exptime ) ? $init : false;
122 } elseif ( $this->isInteger( $curValue ) ) {
123 $newValue = max( $curValue + $step, 0 );
124 $this->bag[$key][self::KEY_VAL] = $newValue;
125 } else {
126 $newValue = false;
127 }
128
129 return $newValue;
130 }
131
135 public function clear() {
136 $this->bag = [];
137 }
138
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
163 public function hasKey( $key ) {
164 return isset( $this->bag[$key] );
165 }
166}
167
169class_alias( HashBagOStuff::class, 'HashBagOStuff' );
const ATTR_DURABILITY
Key in getQoS() for durability of storage writes.
const QOS_DURABILITY_SCRIPT
Storage survives in memory until the end of the current request or CLI process (HashBagOStuff).
Store data in a memory for the current request/process only.
doSet( $key, $value, $exptime=0, $flags=0)
Set an item.bool Success
doGet( $key, $flags=0, &$casToken=null)
Get an item.The CAS token should be null if the key does not exist or the value is corruptmixed Retur...
doIncrWithInit( $key, $exptime, $step, $init, $flags)
int|bool New value or false on failure
doDelete( $key, $flags=0)
Delete an item.bool True if the item was deleted or not found, false on failure
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.bool Success
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.