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