Merge "Fix fatal due to lock name mismatch in Maintenance::unlockSearchindex"
[lhc/web/wiklou.git] / includes / libs / objectcache / WinCacheBagOStuff.php
1 <?php
2 /**
3 * Object caching using WinCache.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Cache
22 */
23
24 /**
25 * Wrapper for WinCache object caching functions; identical interface
26 * to the APC wrapper
27 *
28 * @ingroup Cache
29 */
30 class WinCacheBagOStuff extends BagOStuff {
31 protected function doGet( $key, $flags = 0 ) {
32 $blob = wincache_ucache_get( $key );
33
34 return is_string( $blob ) ? unserialize( $blob ) : false;
35 }
36
37 protected function getWithToken( $key, &$casToken, $flags = 0 ) {
38 $casToken = null;
39
40 $blob = wincache_ucache_get( $key );
41 if ( !is_string( $blob ) ) {
42 return false;
43 }
44
45 $value = unserialize( $blob );
46 if ( $value === false ) {
47 return false;
48 }
49
50 $casToken = $blob; // don't bother hashing this
51
52 return $value;
53 }
54
55 protected function cas( $casToken, $key, $value, $exptime = 0, $flags = 0 ) {
56 if ( !wincache_lock( $key ) ) { // optimize with FIFO lock
57 return false;
58 }
59
60 $curCasToken = null; // passed by reference
61 $this->getWithToken( $key, $curCasToken, self::READ_LATEST );
62 if ( $casToken === $curCasToken ) {
63 $success = $this->set( $key, $value, $exptime, $flags );
64 } else {
65 $this->logger->info(
66 __METHOD__ . ' failed due to race condition for {key}.',
67 [ 'key' => $key ]
68 );
69
70 $success = false; // mismatched or failed
71 }
72
73 wincache_unlock( $key );
74
75 return $success;
76 }
77
78 public function set( $key, $value, $expire = 0, $flags = 0 ) {
79 $result = wincache_ucache_set( $key, serialize( $value ), $expire );
80
81 return ( $result === [] || $result === true );
82 }
83
84 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
85 $result = wincache_ucache_add( $key, serialize( $value ), $exptime );
86
87 return ( $result === [] || $result === true );
88 }
89
90 public function delete( $key, $flags = 0 ) {
91 wincache_ucache_delete( $key );
92
93 return true;
94 }
95
96 public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
97 return $this->mergeViaCas( $key, $callback, $exptime, $attempts, $flags );
98 }
99
100 /**
101 * Construct a cache key.
102 *
103 * @since 1.27
104 * @param string $keyspace
105 * @param array $args
106 * @return string
107 */
108 public function makeKeyInternal( $keyspace, $args ) {
109 // WinCache keys have a maximum length of 150 characters. From that,
110 // subtract the number of characters we need for the keyspace and for
111 // the separator character needed for each argument. To handle some
112 // custom prefixes used by thing like WANObjectCache, limit to 125.
113 // NOTE: Same as in memcached, except the max key length there is 255.
114 $charsLeft = 125 - strlen( $keyspace ) - count( $args );
115
116 $args = array_map(
117 function ( $arg ) use ( &$charsLeft ) {
118 // 33 = 32 characters for the MD5 + 1 for the '#' prefix.
119 if ( $charsLeft > 33 && strlen( $arg ) > $charsLeft ) {
120 $arg = '#' . md5( $arg );
121 }
122
123 $charsLeft -= strlen( $arg );
124 return $arg;
125 },
126 $args
127 );
128
129 if ( $charsLeft < 0 ) {
130 return $keyspace . ':BagOStuff-long-key:##' . md5( implode( ':', $args ) );
131 }
132
133 return $keyspace . ':' . implode( ':', $args );
134 }
135
136 /**
137 * Increase stored value of $key by $value while preserving its original TTL
138 * @param string $key Key to increase
139 * @param int $value Value to add to $key (Default 1)
140 * @return int|bool New value or false on failure
141 */
142 public function incr( $key, $value = 1 ) {
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 }