Merge "Revert "Live preview no longer experimental""
[lhc/web/wiklou.git] / includes / objectcache / XCacheBagOStuff.php
1 <?php
2 /**
3 * Object caching using XCache.
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 XCache object caching functions; identical interface
26 * to the APC wrapper
27 *
28 * @ingroup Cache
29 */
30 class XCacheBagOStuff extends BagOStuff {
31 /**
32 * Get a value from the XCache object cache
33 *
34 * @param $key String: cache key
35 * @param $casToken mixed: cas token
36 * @return mixed
37 */
38 public function get( $key, &$casToken = null ) {
39 $val = xcache_get( $key );
40
41 if ( is_string( $val ) ) {
42 if ( $this->isInteger( $val ) ) {
43 $val = intval( $val );
44 } else {
45 $val = unserialize( $val );
46 }
47 }
48
49 return $val;
50 }
51
52 /**
53 * Store a value in the XCache object cache
54 *
55 * @param $key String: cache key
56 * @param $value Mixed: object to store
57 * @param $expire Int: expiration time
58 * @return bool
59 */
60 public function set( $key, $value, $expire = 0 ) {
61 if ( !$this->isInteger( $value ) ) {
62 $value = serialize( $value );
63 }
64
65 xcache_set( $key, $value, $expire );
66 return true;
67 }
68
69 /**
70 * @param $casToken mixed
71 * @param $key string
72 * @param $value mixed
73 * @param $exptime int
74 * @return bool
75 */
76 public function cas( $casToken, $key, $value, $exptime = 0 ) {
77 // Can't find any documentation on xcache cas
78 throw new MWException( "CAS is not implemented in " . __CLASS__ );
79 }
80
81 /**
82 * Remove a value from the XCache object cache
83 *
84 * @param $key String: cache key
85 * @param $time Int: not used in this implementation
86 * @return bool
87 */
88 public function delete( $key, $time = 0 ) {
89 xcache_unset( $key );
90 return true;
91 }
92
93 /**
94 * Merge an item.
95 * XCache does not seem to support any way of performing CAS - this however will
96 * provide a way to perform CAS-like functionality.
97 *
98 * @param $key string
99 * @param $callback closure Callback method to be executed
100 * @param $exptime int Either an interval in seconds or a unix timestamp for expiry
101 * @param $attempts int The amount of times to attempt a merge in case of failure
102 * @return bool success
103 */
104 public function merge( $key, closure $callback, $exptime = 0, $attempts = 10 ) {
105 return $this->mergeViaLock( $key, $callback, $exptime, $attempts );
106 }
107
108 public function incr( $key, $value = 1 ) {
109 return xcache_inc( $key, $value );
110 }
111
112 public function decr( $key, $value = 1 ) {
113 return xcache_dec( $key, $value );
114 }
115 }