MediaWiki  master
WinCacheBagOStuff.php
Go to the documentation of this file.
1 <?php
31  public function __construct( array $params = [] ) {
32  $params['segmentationSize'] = $params['segmentationSize'] ?? INF;
33  parent::__construct( $params );
34 
35  if ( PHP_SAPI === 'cli' ) {
36  $this->attrMap[self::ATTR_DURABILITY] = ini_get( 'wincache.enablecli' )
39  } else {
41  }
42  }
43 
44  protected function doGet( $key, $flags = 0, &$casToken = null ) {
45  $getToken = ( $casToken === self::PASS_BY_REF );
46  $casToken = null;
47 
48  $blob = wincache_ucache_get( $key );
49  if ( !is_string( $blob ) && !is_int( $blob ) ) {
50  return false;
51  }
52 
53  $value = $this->unserialize( $blob );
54  if ( $getToken && $value !== false ) {
55  $casToken = (string)$blob; // don't bother hashing this
56  }
57 
58  return $value;
59  }
60 
61  protected function doCas( $casToken, $key, $value, $exptime = 0, $flags = 0 ) {
62  if ( !wincache_lock( $key ) ) { // optimize with FIFO lock
63  return false;
64  }
65 
66  $curCasToken = self::PASS_BY_REF; // passed by reference
67  $this->doGet( $key, self::READ_LATEST, $curCasToken );
68  if ( $casToken === $curCasToken ) {
69  $success = $this->set( $key, $value, $exptime, $flags );
70  } else {
71  $this->logger->info(
72  __METHOD__ . ' failed due to race condition for {key}.',
73  [ 'key' => $key ]
74  );
75 
76  $success = false; // mismatched or failed
77  }
78 
79  wincache_unlock( $key );
80 
81  return $success;
82  }
83 
84  protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
85  $ttl = $this->getExpirationAsTTL( $exptime );
86  $result = wincache_ucache_set( $key, $this->getSerialized( $value, $key ), $ttl );
87 
88  // false positive, wincache_ucache_set returns an empty array
89  // in some circumstances.
90  // @phan-suppress-next-line PhanTypeComparisonToArray
91  return ( $result === [] || $result === true );
92  }
93 
94  protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
95  if ( wincache_ucache_exists( $key ) ) {
96  return false; // avoid warnings
97  }
98 
99  $ttl = $this->getExpirationAsTTL( $exptime );
100  $result = wincache_ucache_add( $key, $this->getSerialized( $value, $key ), $ttl );
101 
102  // false positive, wincache_ucache_add returns an empty array
103  // in some circumstances.
104  // @phan-suppress-next-line PhanTypeComparisonToArray
105  return ( $result === [] || $result === true );
106  }
107 
108  protected function doDelete( $key, $flags = 0 ) {
109  wincache_ucache_delete( $key );
110 
111  return true;
112  }
113 
114  public function makeKeyInternal( $keyspace, $components ) {
115  // WinCache keys have a maximum length of 150 characters. From that,
116  // subtract the number of characters we need for the keyspace and for
117  // the separator character needed for each argument. To handle some
118  // custom prefixes used by thing like WANObjectCache, limit to 125.
119  // NOTE: Same as in memcached, except the max key length there is 255.
120  $charsLeft = 125 - strlen( $keyspace ) - count( $components );
121 
122  $components = array_map(
123  static function ( $component ) use ( &$charsLeft ) {
124  // 33 = 32 characters for the MD5 + 1 for the '#' prefix.
125  if ( $charsLeft > 33 && strlen( $component ) > $charsLeft ) {
126  $component = '#' . md5( $component );
127  }
128 
129  $charsLeft -= strlen( $component );
130  return $component;
131  },
132  $components
133  );
134 
135  if ( $charsLeft < 0 ) {
136  return $keyspace . ':BagOStuff-long-key:##' . md5( implode( ':', $components ) );
137  }
138 
139  return $keyspace . ':' . implode( ':', $components );
140  }
141 
142  public function doIncrWithInit( $key, $exptime, $step, $init, $flags ) {
143  if ( !wincache_lock( $key ) ) { // optimize with FIFO lock
144  return false;
145  }
146 
147  $curValue = $this->doGet( $key );
148  if ( $curValue === false ) {
149  $newValue = $this->doSet( $key, $init, $exptime ) ? $init : false;
150  } elseif ( $this->isInteger( $curValue ) ) {
151  $sum = max( $curValue + $step, 0 );
152  $oldTTL = wincache_ucache_info( false, $key )["ucache_entries"][1]["ttl_seconds"];
153  $newValue = $this->doSet( $key, $sum, $oldTTL ) ? $sum : false;
154  } else {
155  $newValue = false;
156  }
157 
158  wincache_unlock( $key );
159 
160  return $newValue;
161  }
162 
163  public function incr( $key, $value = 1, $flags = 0 ) {
164  return $this->doIncr( $key, $value, $flags );
165  }
166 
167  public function decr( $key, $value = 1, $flags = 0 ) {
168  return $this->doIncr( $key, -$value, $flags );
169  }
170 
171  private function doIncr( $key, $value = 1, $flags = 0 ) {
172  if ( !wincache_lock( $key ) ) { // optimize with FIFO lock
173  return false;
174  }
175 
176  $n = $this->doGet( $key );
177  if ( $this->isInteger( $n ) ) {
178  $n = max( $n + (int)$value, 0 );
179  $oldTTL = wincache_ucache_info( false, $key )["ucache_entries"][1]["ttl_seconds"];
180  $this->doSet( $key, $n, $oldTTL );
181  } else {
182  $n = false;
183  }
184 
185  wincache_unlock( $key );
186 
187  return $n;
188  }
189 }
$success
string $keyspace
Default keyspace; used by makeKey()
Definition: BagOStuff.php:104
Storage medium specific cache for storing items (e.g.
getSerialized( $value, $key)
Get the serialized form a value, using any applicable prepared value.
getExpirationAsTTL( $exptime)
Convert an optionally absolute expiry time to a relative time.
isInteger( $value)
Check if a value is an integer.
Wrapper for WinCache object caching functions; identical interface to the APC wrapper.
doDelete( $key, $flags=0)
Delete an item.
doGet( $key, $flags=0, &$casToken=null)
doCas( $casToken, $key, $value, $exptime=0, $flags=0)
Check and set an item.
doAdd( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
decr( $key, $value=1, $flags=0)
Decrease stored value of $key by $value while preserving its TTL.
doSet( $key, $value, $exptime=0, $flags=0)
Set an item.
__construct(array $params=[])
doIncrWithInit( $key, $exptime, $step, $init, $flags)
makeKeyInternal( $keyspace, $components)
Make a cache key for the given keyspace and components.
incr( $key, $value=1, $flags=0)
Increase stored value of $key by $value while preserving its TTL.
doIncr( $key, $value=1, $flags=0)
const QOS_DURABILITY_SERVICE
Data is lost once the service storing the data restarts.
const QOS_DURABILITY_SCRIPT
Data is lost at the end of the current web request or CLI script.
const QOS_DURABILITY_NONE
Data is never saved to begin with (blackhole store)
const ATTR_DURABILITY
Durability of writes; see QOS_DURABILITY_* (higher means stronger)