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 
36  protected function doGet( $key, $flags = 0, &$casToken = null ) {
37  $getToken = ( $casToken === self::PASS_BY_REF );
38  $casToken = null;
39 
40  $blob = wincache_ucache_get( $key );
41  if ( !is_string( $blob ) && !is_int( $blob ) ) {
42  return false;
43  }
44 
45  $value = $this->unserialize( $blob );
46  if ( $getToken && $value !== false ) {
47  $casToken = (string)$blob; // don't bother hashing this
48  }
49 
50  return $value;
51  }
52 
53  protected function doCas( $casToken, $key, $value, $exptime = 0, $flags = 0 ) {
54  if ( !wincache_lock( $key ) ) { // optimize with FIFO lock
55  return false;
56  }
57 
58  $curCasToken = self::PASS_BY_REF; // passed by reference
59  $this->doGet( $key, self::READ_LATEST, $curCasToken );
60  if ( $casToken === $curCasToken ) {
61  $success = $this->set( $key, $value, $exptime, $flags );
62  } else {
63  $this->logger->info(
64  __METHOD__ . ' failed due to race condition for {key}.',
65  [ 'key' => $key ]
66  );
67 
68  $success = false; // mismatched or failed
69  }
70 
71  wincache_unlock( $key );
72 
73  return $success;
74  }
75 
76  protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
77  $result = wincache_ucache_set( $key, $this->getSerialized( $value, $key ), $exptime );
78 
79  // false positive, wincache_ucache_set returns an empty array
80  // in some circumstances.
81  // @phan-suppress-next-line PhanTypeComparisonToArray
82  return ( $result === [] || $result === true );
83  }
84 
85  protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
86  if ( wincache_ucache_exists( $key ) ) {
87  return false; // avoid warnings
88  }
89 
90  $result = wincache_ucache_add( $key, $this->getSerialized( $value, $key ), $exptime );
91 
92  // false positive, wincache_ucache_add returns an empty array
93  // in some circumstances.
94  // @phan-suppress-next-line PhanTypeComparisonToArray
95  return ( $result === [] || $result === true );
96  }
97 
98  protected function doDelete( $key, $flags = 0 ) {
99  wincache_ucache_delete( $key );
100 
101  return true;
102  }
103 
104  public function makeKeyInternal( $keyspace, $args ) {
105  // WinCache keys have a maximum length of 150 characters. From that,
106  // subtract the number of characters we need for the keyspace and for
107  // the separator character needed for each argument. To handle some
108  // custom prefixes used by thing like WANObjectCache, limit to 125.
109  // NOTE: Same as in memcached, except the max key length there is 255.
110  $charsLeft = 125 - strlen( $keyspace ) - count( $args );
111 
112  $args = array_map(
113  function ( $arg ) use ( &$charsLeft ) {
114  // 33 = 32 characters for the MD5 + 1 for the '#' prefix.
115  if ( $charsLeft > 33 && strlen( $arg ) > $charsLeft ) {
116  $arg = '#' . md5( $arg );
117  }
118 
119  $charsLeft -= strlen( $arg );
120  return $arg;
121  },
122  $args
123  );
124 
125  if ( $charsLeft < 0 ) {
126  return $keyspace . ':BagOStuff-long-key:##' . md5( implode( ':', $args ) );
127  }
128 
129  return $keyspace . ':' . implode( ':', $args );
130  }
131 
132  public function incr( $key, $value = 1, $flags = 0 ) {
133  if ( !wincache_lock( $key ) ) { // optimize with FIFO lock
134  return false;
135  }
136 
137  $n = $this->doGet( $key );
138  if ( $this->isInteger( $n ) ) {
139  $n = max( $n + (int)$value, 0 );
140  $oldTTL = wincache_ucache_info( false, $key )["ucache_entries"][1]["ttl_seconds"];
141  $this->set( $key, $n, $oldTTL );
142  } else {
143  $n = false;
144  }
145 
146  wincache_unlock( $key );
147 
148  return $n;
149  }
150 
151  public function decr( $key, $value = 1, $flags = 0 ) {
152  return $this->incr( $key, -$value, $flags );
153  }
154 }
MediumSpecificBagOStuff\isInteger
isInteger( $value)
Check if a value is an integer.
Definition: MediumSpecificBagOStuff.php:881
MediumSpecificBagOStuff\$keyspace
string $keyspace
Definition: MediumSpecificBagOStuff.php:40
WinCacheBagOStuff\__construct
__construct(array $params=[])
Definition: WinCacheBagOStuff.php:31
WinCacheBagOStuff\doDelete
doDelete( $key, $flags=0)
Delete an item.
Definition: WinCacheBagOStuff.php:98
WinCacheBagOStuff\doAdd
doAdd( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
Definition: WinCacheBagOStuff.php:85
WinCacheBagOStuff\doGet
doGet( $key, $flags=0, &$casToken=null)
Definition: WinCacheBagOStuff.php:36
$success
$success
Definition: NoLocalSettings.php:42
WinCacheBagOStuff\makeKeyInternal
makeKeyInternal( $keyspace, $args)
Construct a cache key.
Definition: WinCacheBagOStuff.php:104
WinCacheBagOStuff\decr
decr( $key, $value=1, $flags=0)
Decrease stored value of $key by $value while preserving its TTL.
Definition: WinCacheBagOStuff.php:151
$blob
$blob
Definition: testCompression.php:70
$args
if( $line===false) $args
Definition: mcc.php:124
WinCacheBagOStuff\doCas
doCas( $casToken, $key, $value, $exptime=0, $flags=0)
Check and set an item.
Definition: WinCacheBagOStuff.php:53
MediumSpecificBagOStuff
Storage medium specific cache for storing items (e.g.
Definition: MediumSpecificBagOStuff.php:34
MediumSpecificBagOStuff\getSerialized
getSerialized( $value, $key)
Get the serialized form a value, using any applicable prepared value.
Definition: MediumSpecificBagOStuff.php:971
WinCacheBagOStuff\doSet
doSet( $key, $value, $exptime=0, $flags=0)
Set an item.
Definition: WinCacheBagOStuff.php:76
WinCacheBagOStuff
Wrapper for WinCache object caching functions; identical interface to the APC wrapper.
Definition: WinCacheBagOStuff.php:30
MediumSpecificBagOStuff\unserialize
unserialize( $value)
Definition: MediumSpecificBagOStuff.php:1057
WinCacheBagOStuff\incr
incr( $key, $value=1, $flags=0)
Increase stored value of $key by $value while preserving its TTL.
Definition: WinCacheBagOStuff.php:132