MediaWiki REL1_33
HashBagOStuff.php
Go to the documentation of this file.
1<?php
31class HashBagOStuff extends BagOStuff {
33 protected $bag = [];
35 protected $maxCacheKeys;
36
38 private $token;
39
41 private static $casCounter = 0;
42
43 const KEY_VAL = 0;
44 const KEY_EXP = 1;
45 const KEY_CAS = 2;
46
51 function __construct( $params = [] ) {
52 parent::__construct( $params );
53
54 $this->token = microtime( true ) . ':' . mt_rand();
55 $this->maxCacheKeys = $params['maxKeys'] ?? INF;
56 if ( $this->maxCacheKeys <= 0 ) {
57 throw new InvalidArgumentException( '$maxKeys parameter must be above zero' );
58 }
59 }
60
61 protected function doGet( $key, $flags = 0, &$casToken = null ) {
62 $casToken = null;
63
64 if ( !$this->hasKey( $key ) || $this->expire( $key ) ) {
65 return false;
66 }
67
68 // Refresh key position for maxCacheKeys eviction
69 $temp = $this->bag[$key];
70 unset( $this->bag[$key] );
71 $this->bag[$key] = $temp;
72
73 $casToken = $this->bag[$key][self::KEY_CAS];
74
75 return $this->bag[$key][self::KEY_VAL];
76 }
77
78 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
79 // Refresh key position for maxCacheKeys eviction
80 unset( $this->bag[$key] );
81 $this->bag[$key] = [
82 self::KEY_VAL => $value,
83 self::KEY_EXP => $this->convertToExpiry( $exptime ),
84 self::KEY_CAS => $this->token . ':' . ++self::$casCounter
85 ];
86
87 if ( count( $this->bag ) > $this->maxCacheKeys ) {
88 reset( $this->bag );
89 $evictKey = key( $this->bag );
90 unset( $this->bag[$evictKey] );
91 }
92
93 return true;
94 }
95
96 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
97 if ( $this->get( $key ) === false ) {
98 return $this->set( $key, $value, $exptime, $flags );
99 }
100
101 return false; // key already set
102 }
103
104 public function delete( $key, $flags = 0 ) {
105 unset( $this->bag[$key] );
106
107 return true;
108 }
109
110 public function incr( $key, $value = 1 ) {
111 $n = $this->get( $key );
112 if ( $this->isInteger( $n ) ) {
113 $n = max( $n + intval( $value ), 0 );
114 $this->bag[$key][self::KEY_VAL] = $n;
115
116 return $n;
117 }
118
119 return false;
120 }
121
125 public function clear() {
126 $this->bag = [];
127 }
128
133 protected function expire( $key ) {
134 $et = $this->bag[$key][self::KEY_EXP];
135 if ( $et == self::TTL_INDEFINITE || $et > $this->getCurrentTime() ) {
136 return false;
137 }
138
139 $this->delete( $key );
140
141 return true;
142 }
143
151 protected function hasKey( $key ) {
152 return isset( $this->bag[$key] );
153 }
154}
Class representing a cache/ephemeral data store.
Definition BagOStuff.php:58
isInteger( $value)
Check if a value is an integer.
convertToExpiry( $exptime)
Convert an optionally relative time to an absolute time.
Simple store for keeping values in an associative array for the current process.
string $token
CAS token prefix for this instance.
incr( $key, $value=1)
Increase stored value of $key by $value while preserving its TTL.
clear()
Clear all values in cache.
add( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
hasKey( $key)
Does this bag have a non-null value for the given key?
__construct( $params=[])
doGet( $key, $flags=0, &$casToken=null)
static int $casCounter
CAS token counter.
int $maxCacheKeys
Max entries allowed.
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message key
Definition hooks.txt:2163
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
$params