MediaWiki  master
WinCacheBagOStuff.php
Go to the documentation of this file.
1 <?php
31  public function __construct( array $params = [] ) {
32  $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  $data = wincache_ucache_get( $key );
49  if ( !is_string( $data ) && !is_int( $data ) ) {
50  return false;
51  }
52 
53  $value = $this->unserialize( $data );
54  if ( $getToken && $value !== false ) {
55  $casToken = $data;
56  }
57 
58  return $value;
59  }
60 
61  protected function doCas( $casToken, $key, $value, $exptime = 0, $flags = 0 ) {
62  // optimize with FIFO lock
63  if ( !wincache_lock( $key ) ) {
64  return false;
65  }
66 
67  $curCasToken = self::PASS_BY_REF;
68  $this->doGet( $key, self::READ_LATEST, $curCasToken );
69  if ( $casToken === $curCasToken ) {
70  $success = $this->set( $key, $value, $exptime, $flags );
71  } else {
72  $this->logger->info(
73  __METHOD__ . ' failed due to race condition for {key}.',
74  [ 'key' => $key ]
75  );
76 
77  // mismatched or failed
78  $success = false;
79  }
80 
81  wincache_unlock( $key );
82 
83  return $success;
84  }
85 
86  protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
87  $ttl = $this->getExpirationAsTTL( $exptime );
88  $result = wincache_ucache_set( $key, $this->getSerialized( $value, $key ), $ttl );
89 
90  // false positive, wincache_ucache_set returns an empty array
91  // in some circumstances.
92  // @phan-suppress-next-line PhanTypeComparisonToArray
93  return ( $result === [] || $result === true );
94  }
95 
96  protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
97  if ( wincache_ucache_exists( $key ) ) {
98  // avoid warnings
99  return false;
100  }
101 
102  $ttl = $this->getExpirationAsTTL( $exptime );
103  $result = wincache_ucache_add( $key, $this->getSerialized( $value, $key ), $ttl );
104 
105  // false positive, wincache_ucache_add returns an empty array
106  // in some circumstances.
107  // @phan-suppress-next-line PhanTypeComparisonToArray
108  return ( $result === [] || $result === true );
109  }
110 
111  protected function doDelete( $key, $flags = 0 ) {
112  wincache_ucache_delete( $key );
113 
114  return true;
115  }
116 
117  public function makeKeyInternal( $keyspace, $components ) {
118  // WinCache keys have a maximum length of 150 characters. From that,
119  // subtract the number of characters we need for the keyspace and for
120  // the separator character needed for each argument. To handle some
121  // custom prefixes used by thing like WANObjectCache, limit to 125.
122  // NOTE: Same as in memcached, except the max key length there is 255.
123  $charsLeft = 125 - strlen( $keyspace ) - count( $components );
124 
125  $components = array_map(
126  static function ( $component ) use ( &$charsLeft ) {
127  // 33 = 32 characters for the MD5 + 1 for the '#' prefix.
128  if ( $charsLeft > 33 && strlen( $component ) > $charsLeft ) {
129  $component = '#' . md5( $component );
130  }
131 
132  $charsLeft -= strlen( $component );
133  return $component;
134  },
135  $components
136  );
137 
138  if ( $charsLeft < 0 ) {
139  return $keyspace . ':BagOStuff-long-key:##' . md5( implode( ':', $components ) );
140  }
141 
142  return $keyspace . ':' . implode( ':', $components );
143  }
144 
145  public function doIncrWithInit( $key, $exptime, $step, $init, $flags ) {
146  // optimize with FIFO lock
147  if ( !wincache_lock( $key ) ) {
148  return false;
149  }
150 
151  $curValue = $this->doGet( $key );
152  if ( $curValue === false ) {
153  $newValue = $this->doSet( $key, $init, $exptime ) ? $init : false;
154  } elseif ( $this->isInteger( $curValue ) ) {
155  $sum = max( $curValue + $step, 0 );
156  $oldTTL = wincache_ucache_info( false, $key )["ucache_entries"][1]["ttl_seconds"];
157  $newValue = $this->doSet( $key, $sum, $oldTTL ) ? $sum : false;
158  } else {
159  $newValue = false;
160  }
161 
162  wincache_unlock( $key );
163 
164  return $newValue;
165  }
166 
167  public function incr( $key, $value = 1, $flags = 0 ) {
168  return $this->doIncr( $key, $value, $flags );
169  }
170 
171  public function decr( $key, $value = 1, $flags = 0 ) {
172  return $this->doIncr( $key, -$value, $flags );
173  }
174 
175  private function doIncr( $key, $value = 1, $flags = 0 ) {
176  // optimize with FIFO lock
177  if ( !wincache_lock( $key ) ) {
178  return false;
179  }
180 
181  $n = $this->doGet( $key );
182  if ( $this->isInteger( $n ) ) {
183  $n = max( $n + (int)$value, 0 );
184  $oldTTL = wincache_ucache_info( false, $key )["ucache_entries"][1]["ttl_seconds"];
185  $this->doSet( $key, $n, $oldTTL );
186  } else {
187  $n = false;
188  }
189 
190  wincache_unlock( $key );
191 
192  return $n;
193  }
194 }
$success
string $keyspace
Default keyspace; used by makeKey()
Definition: BagOStuff.php:101
Storage medium specific cache for storing items (e.g.
const PASS_BY_REF
Idiom for doGet() to return extra information by reference.
getSerialized( $value, $key)
Get the serialized form a value, logging a warning if it involves custom classes.
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)
Get an item.
doCas( $casToken, $key, $value, $exptime=0, $flags=0)
Set an item if the current CAS token matches the provided CAS token.
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.
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)