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