HISTORY: Add MediaWiki 1.12 post-release change notes
[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 $val = wincache_ucache_get( $key );
33 if ( is_string( $val ) ) {
34 $val = unserialize( $val );
35 }
36
37 return $val;
38 }
39
40 public function set( $key, $value, $expire = 0, $flags = 0 ) {
41 $result = wincache_ucache_set( $key, serialize( $value ), $expire );
42
43 return ( $result === [] || $result === true );
44 }
45
46 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
47 $result = wincache_ucache_add( $key, serialize( $value ), $exptime );
48
49 return ( $result === [] || $result === true );
50 }
51
52 public function delete( $key, $flags = 0 ) {
53 wincache_ucache_delete( $key );
54
55 return true;
56 }
57
58 public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
59 if ( wincache_lock( $key ) ) { // optimize with FIFO lock
60 $ok = $this->mergeViaLock( $key, $callback, $exptime, $attempts, $flags );
61 wincache_unlock( $key );
62 } else {
63 $ok = false;
64 }
65
66 return $ok;
67 }
68
69 /**
70 * Construct a cache key.
71 *
72 * @since 1.27
73 * @param string $keyspace
74 * @param array $args
75 * @return string
76 */
77 public function makeKeyInternal( $keyspace, $args ) {
78 // WinCache keys have a maximum length of 150 characters. From that,
79 // subtract the number of characters we need for the keyspace and for
80 // the separator character needed for each argument. To handle some
81 // custom prefixes used by thing like WANObjectCache, limit to 125.
82 // NOTE: Same as in memcached, except the max key length there is 255.
83 $charsLeft = 125 - strlen( $keyspace ) - count( $args );
84
85 $args = array_map(
86 function ( $arg ) use ( &$charsLeft ) {
87 // 33 = 32 characters for the MD5 + 1 for the '#' prefix.
88 if ( $charsLeft > 33 && strlen( $arg ) > $charsLeft ) {
89 $arg = '#' . md5( $arg );
90 }
91
92 $charsLeft -= strlen( $arg );
93 return $arg;
94 },
95 $args
96 );
97
98 if ( $charsLeft < 0 ) {
99 return $keyspace . ':BagOStuff-long-key:##' . md5( implode( ':', $args ) );
100 }
101
102 return $keyspace . ':' . implode( ':', $args );
103 }
104
105 /**
106 * Increase stored value of $key by $value while preserving its original TTL
107 * @param string $key Key to increase
108 * @param int $value Value to add to $key (Default 1)
109 * @return int|bool New value or false on failure
110 */
111 public function incr( $key, $value = 1 ) {
112 if ( !$this->lock( $key ) ) {
113 return false;
114 }
115 $n = $this->get( $key );
116 if ( $this->isInteger( $n ) ) { // key exists?
117 $n += intval( $value );
118 $oldTTL = wincache_ucache_info( false, $key )["ucache_entries"][1]["ttl_seconds"];
119 $this->set( $key, max( 0, $n ), $oldTTL );
120 } else {
121 $n = false;
122 }
123 $this->unlock( $key );
124
125 return $n;
126 }
127 }