MediaWiki REL1_35
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 $ttl = $this->getExpirationAsTTL( $exptime );
77 $result = wincache_ucache_set( $key, $this->getSerialized( $value, $key ), $ttl );
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 $ttl = $this->getExpirationAsTTL( $exptime );
91 $result = wincache_ucache_add( $key, $this->getSerialized( $value, $key ), $ttl );
92
93 // false positive, wincache_ucache_add returns an empty array
94 // in some circumstances.
95 // @phan-suppress-next-line PhanTypeComparisonToArray
96 return ( $result === [] || $result === true );
97 }
98
99 protected function doDelete( $key, $flags = 0 ) {
100 wincache_ucache_delete( $key );
101
102 return true;
103 }
104
105 public function makeKeyInternal( $keyspace, $args ) {
106 // WinCache keys have a maximum length of 150 characters. From that,
107 // subtract the number of characters we need for the keyspace and for
108 // the separator character needed for each argument. To handle some
109 // custom prefixes used by thing like WANObjectCache, limit to 125.
110 // NOTE: Same as in memcached, except the max key length there is 255.
111 $charsLeft = 125 - strlen( $keyspace ) - count( $args );
112
113 $args = array_map(
114 function ( $arg ) use ( &$charsLeft ) {
115 // 33 = 32 characters for the MD5 + 1 for the '#' prefix.
116 if ( $charsLeft > 33 && strlen( $arg ) > $charsLeft ) {
117 $arg = '#' . md5( $arg );
118 }
119
120 $charsLeft -= strlen( $arg );
121 return $arg;
122 },
123 $args
124 );
125
126 if ( $charsLeft < 0 ) {
127 return $keyspace . ':BagOStuff-long-key:##' . md5( implode( ':', $args ) );
128 }
129
130 return $keyspace . ':' . implode( ':', $args );
131 }
132
133 public function incr( $key, $value = 1, $flags = 0 ) {
134 if ( !wincache_lock( $key ) ) { // optimize with FIFO lock
135 return false;
136 }
137
138 $n = $this->doGet( $key );
139 if ( $this->isInteger( $n ) ) {
140 $n = max( $n + (int)$value, 0 );
141 $oldTTL = wincache_ucache_info( false, $key )["ucache_entries"][1]["ttl_seconds"];
142 $this->set( $key, $n, $oldTTL );
143 } else {
144 $n = false;
145 }
146
147 wincache_unlock( $key );
148
149 return $n;
150 }
151
152 public function decr( $key, $value = 1, $flags = 0 ) {
153 return $this->incr( $key, -$value, $flags );
154 }
155}
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=[])
incr( $key, $value=1, $flags=0)
Increase stored value of $key by $value while preserving its TTL.
makeKeyInternal( $keyspace, $args)
Construct a cache key.
if( $line===false) $args
Definition mcc.php:124