Merge "Disable expensive {{REVISIONID}} magic word in miser mode"
[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, &$casToken = null ) {
32 $casToken = null;
33
34 $blob = wincache_ucache_get( $key );
35 if ( !is_string( $blob ) ) {
36 return false;
37 }
38
39 $value = unserialize( $blob );
40 if ( $value !== false ) {
41 $casToken = (string)$blob; // don't bother hashing this
42 }
43
44 return $value;
45 }
46
47 protected function cas( $casToken, $key, $value, $exptime = 0, $flags = 0 ) {
48 if ( !wincache_lock( $key ) ) { // optimize with FIFO lock
49 return false;
50 }
51
52 $curCasToken = null; // passed by reference
53 $this->doGet( $key, self::READ_LATEST, $curCasToken );
54 if ( $casToken === $curCasToken ) {
55 $success = $this->set( $key, $value, $exptime, $flags );
56 } else {
57 $this->logger->info(
58 __METHOD__ . ' failed due to race condition for {key}.',
59 [ 'key' => $key ]
60 );
61
62 $success = false; // mismatched or failed
63 }
64
65 wincache_unlock( $key );
66
67 return $success;
68 }
69
70 public function set( $key, $value, $expire = 0, $flags = 0 ) {
71 $result = wincache_ucache_set( $key, serialize( $value ), $expire );
72
73 return ( $result === [] || $result === true );
74 }
75
76 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
77 $result = wincache_ucache_add( $key, serialize( $value ), $exptime );
78
79 return ( $result === [] || $result === true );
80 }
81
82 public function delete( $key, $flags = 0 ) {
83 wincache_ucache_delete( $key );
84
85 return true;
86 }
87
88 /**
89 * Construct a cache key.
90 *
91 * @since 1.27
92 * @param string $keyspace
93 * @param array $args
94 * @return string
95 */
96 public function makeKeyInternal( $keyspace, $args ) {
97 // WinCache keys have a maximum length of 150 characters. From that,
98 // subtract the number of characters we need for the keyspace and for
99 // the separator character needed for each argument. To handle some
100 // custom prefixes used by thing like WANObjectCache, limit to 125.
101 // NOTE: Same as in memcached, except the max key length there is 255.
102 $charsLeft = 125 - strlen( $keyspace ) - count( $args );
103
104 $args = array_map(
105 function ( $arg ) use ( &$charsLeft ) {
106 // 33 = 32 characters for the MD5 + 1 for the '#' prefix.
107 if ( $charsLeft > 33 && strlen( $arg ) > $charsLeft ) {
108 $arg = '#' . md5( $arg );
109 }
110
111 $charsLeft -= strlen( $arg );
112 return $arg;
113 },
114 $args
115 );
116
117 if ( $charsLeft < 0 ) {
118 return $keyspace . ':BagOStuff-long-key:##' . md5( implode( ':', $args ) );
119 }
120
121 return $keyspace . ':' . implode( ':', $args );
122 }
123
124 /**
125 * Increase stored value of $key by $value while preserving its original TTL
126 * @param string $key Key to increase
127 * @param int $value Value to add to $key (Default 1)
128 * @return int|bool New value or false on failure
129 */
130 public function incr( $key, $value = 1 ) {
131 if ( !wincache_lock( $key ) ) { // optimize with FIFO lock
132 return false;
133 }
134
135 $n = $this->doGet( $key );
136 if ( $this->isInteger( $n ) ) {
137 $n = max( $n + (int)$value, 0 );
138 $oldTTL = wincache_ucache_info( false, $key )["ucache_entries"][1]["ttl_seconds"];
139 $this->set( $key, $n, $oldTTL );
140 } else {
141 $n = false;
142 }
143
144 wincache_unlock( $key );
145
146 return $n;
147 }
148 }