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