Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[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 = $this->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 protected function doSet( $key, $value, $expire = 0, $flags = 0 ) {
71 $result = wincache_ucache_set( $key, $this->serialize( $value ), $expire );
72
73 // false positive, wincache_ucache_set returns an empty array
74 // in some circumstances.
75 // @phan-suppress-next-line PhanTypeComparisonToArray
76 return ( $result === [] || $result === true );
77 }
78
79 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
80 if ( wincache_ucache_exists( $key ) ) {
81 return false; // avoid warnings
82 }
83
84 $result = wincache_ucache_add( $key, $this->serialize( $value ), $exptime );
85
86 // false positive, wincache_ucache_add returns an empty array
87 // in some circumstances.
88 // @phan-suppress-next-line PhanTypeComparisonToArray
89 return ( $result === [] || $result === true );
90 }
91
92 protected function doDelete( $key, $flags = 0 ) {
93 wincache_ucache_delete( $key );
94
95 return true;
96 }
97
98 /**
99 * Construct a cache key.
100 *
101 * @since 1.27
102 * @param string $keyspace
103 * @param array $args
104 * @return string
105 */
106 public function makeKeyInternal( $keyspace, $args ) {
107 // WinCache keys have a maximum length of 150 characters. From that,
108 // subtract the number of characters we need for the keyspace and for
109 // the separator character needed for each argument. To handle some
110 // custom prefixes used by thing like WANObjectCache, limit to 125.
111 // NOTE: Same as in memcached, except the max key length there is 255.
112 $charsLeft = 125 - strlen( $keyspace ) - count( $args );
113
114 $args = array_map(
115 function ( $arg ) use ( &$charsLeft ) {
116 // 33 = 32 characters for the MD5 + 1 for the '#' prefix.
117 if ( $charsLeft > 33 && strlen( $arg ) > $charsLeft ) {
118 $arg = '#' . md5( $arg );
119 }
120
121 $charsLeft -= strlen( $arg );
122 return $arg;
123 },
124 $args
125 );
126
127 if ( $charsLeft < 0 ) {
128 return $keyspace . ':BagOStuff-long-key:##' . md5( implode( ':', $args ) );
129 }
130
131 return $keyspace . ':' . implode( ':', $args );
132 }
133
134 /**
135 * Increase stored value of $key by $value while preserving its original TTL
136 * @param string $key Key to increase
137 * @param int $value Value to add to $key (Default 1)
138 * @return int|bool New value or false on failure
139 */
140 public function incr( $key, $value = 1 ) {
141 if ( !wincache_lock( $key ) ) { // optimize with FIFO lock
142 return false;
143 }
144
145 $n = $this->doGet( $key );
146 if ( $this->isInteger( $n ) ) {
147 $n = max( $n + (int)$value, 0 );
148 $oldTTL = wincache_ucache_info( false, $key )["ucache_entries"][1]["ttl_seconds"];
149 $this->set( $key, $n, $oldTTL );
150 } else {
151 $n = false;
152 }
153
154 wincache_unlock( $key );
155
156 return $n;
157 }
158 }