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 incr( $key, $value = 1, $flags = 0 ) {
143  if ( !wincache_lock( $key ) ) { // optimize with FIFO lock
144  return false;
145  }
146 
147  $n = $this->doGet( $key );
148  if ( $this->isInteger( $n ) ) {
149  $n = max( $n + (int)$value, 0 );
150  $oldTTL = wincache_ucache_info( false, $key )["ucache_entries"][1]["ttl_seconds"];
151  $this->set( $key, $n, $oldTTL );
152  } else {
153  $n = false;
154  }
155 
156  wincache_unlock( $key );
157 
158  return $n;
159  }
160 
161  public function decr( $key, $value = 1, $flags = 0 ) {
162  return $this->incr( $key, -$value, $flags );
163  }
164 }
Wikimedia\LightweightObjectStore\StorageAwareness\QOS_DURABILITY_SCRIPT
const QOS_DURABILITY_SCRIPT
Data is lost at the end of the current web request or CLI script.
Definition: StorageAwareness.php:58
MediumSpecificBagOStuff\isInteger
isInteger( $value)
Check if a value is an integer.
Definition: MediumSpecificBagOStuff.php:953
Wikimedia\LightweightObjectStore\StorageAwareness\QOS_DURABILITY_NONE
const QOS_DURABILITY_NONE
Data is never saved to begin with (blackhole store)
Definition: StorageAwareness.php:56
WinCacheBagOStuff\__construct
__construct(array $params=[])
Definition: WinCacheBagOStuff.php:31
WinCacheBagOStuff\doDelete
doDelete( $key, $flags=0)
Delete an item.
Definition: WinCacheBagOStuff.php:108
WinCacheBagOStuff\doAdd
doAdd( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
Definition: WinCacheBagOStuff.php:94
WinCacheBagOStuff\doGet
doGet( $key, $flags=0, &$casToken=null)
Definition: WinCacheBagOStuff.php:44
$success
$success
Definition: NoLocalSettings.php:42
WinCacheBagOStuff\decr
decr( $key, $value=1, $flags=0)
Decrease stored value of $key by $value while preserving its TTL.
Definition: WinCacheBagOStuff.php:161
$blob
$blob
Definition: testCompression.php:70
WinCacheBagOStuff\doCas
doCas( $casToken, $key, $value, $exptime=0, $flags=0)
Check and set an item.
Definition: WinCacheBagOStuff.php:61
MediumSpecificBagOStuff\getExpirationAsTTL
getExpirationAsTTL( $exptime)
Convert an optionally absolute expiry time to a relative time.
Definition: MediumSpecificBagOStuff.php:937
WinCacheBagOStuff\makeKeyInternal
makeKeyInternal( $keyspace, $components)
Make a cache key for the given keyspace and components.
Definition: WinCacheBagOStuff.php:114
MediumSpecificBagOStuff
Storage medium specific cache for storing items (e.g.
Definition: MediumSpecificBagOStuff.php:34
Wikimedia\LightweightObjectStore\StorageAwareness\QOS_DURABILITY_SERVICE
const QOS_DURABILITY_SERVICE
Data is lost once the service storing the data restarts.
Definition: StorageAwareness.php:60
MediumSpecificBagOStuff\getSerialized
getSerialized( $value, $key)
Get the serialized form a value, using any applicable prepared value.
Definition: MediumSpecificBagOStuff.php:1026
Wikimedia\LightweightObjectStore\StorageAwareness\ATTR_DURABILITY
const ATTR_DURABILITY
Durability of writes; see QOS_DURABILITY_* (higher means stronger)
Definition: StorageAwareness.php:45
WinCacheBagOStuff\doSet
doSet( $key, $value, $exptime=0, $flags=0)
Set an item.
Definition: WinCacheBagOStuff.php:84
WinCacheBagOStuff
Wrapper for WinCache object caching functions; identical interface to the APC wrapper.
Definition: WinCacheBagOStuff.php:30
MediumSpecificBagOStuff\unserialize
unserialize( $value)
Definition: MediumSpecificBagOStuff.php:1173
BagOStuff\$keyspace
string $keyspace
Default keyspace; used by makeKey()
Definition: BagOStuff.php:103
WinCacheBagOStuff\incr
incr( $key, $value=1, $flags=0)
Increase stored value of $key by $value while preserving its TTL.
Definition: WinCacheBagOStuff.php:142