Merge "[JobQueue] Update "empty" cache key when jobs are recycled."
[mediawiki.git] / includes / objectcache / APCBagOStuff.php
blob6c558ce156978a54adad800feffaf096faeb9e5f
1 <?php
2 /**
3 * Object caching using PHP's APC accelerator.
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.
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.
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
20 * @file
21 * @ingroup Cache
24 /**
25 * This is a wrapper for APC's shared memory functions
27 * @ingroup Cache
29 class APCBagOStuff extends BagOStuff {
30 /**
31 * @param $key string
32 * @param $casToken[optional] int
33 * @return mixed
35 public function get( $key, &$casToken = null ) {
36 $val = apc_fetch( $key );
38 $casToken = $val;
40 if ( is_string( $val ) ) {
41 if ( $this->isInteger( $val ) ) {
42 $val = intval( $val );
43 } else {
44 $val = unserialize( $val );
48 return $val;
51 /**
52 * @param $key string
53 * @param $value mixed
54 * @param $exptime int
55 * @return bool
57 public function set( $key, $value, $exptime = 0 ) {
58 if ( !$this->isInteger( $value ) ) {
59 $value = serialize( $value );
62 apc_store( $key, $value, $exptime );
64 return true;
67 /**
68 * @param $casToken mixed
69 * @param $key string
70 * @param $value mixed
71 * @param $exptime int
72 * @return bool
74 public function cas( $casToken, $key, $value, $exptime = 0 ) {
75 // APC's CAS functions only work on integers
76 throw new MWException( "CAS is not implemented in " . __CLASS__ );
79 /**
80 * @param $key string
81 * @param $time int
82 * @return bool
84 public function delete( $key, $time = 0 ) {
85 apc_delete( $key );
87 return true;
90 /**
91 * @param $key string
92 * @param $callback closure Callback method to be executed
93 * @param $exptime int Either an interval in seconds or a unix timestamp for expiry
94 * @param $attempts int The amount of times to attempt a merge in case of failure
95 * @return bool success
97 public function merge( $key, closure $callback, $exptime = 0, $attempts = 10 ) {
98 return $this->mergeViaLock( $key, $callback, $exptime, $attempts );
101 public function incr( $key, $value = 1 ) {
102 return apc_inc( $key, $value );
105 public function decr( $key, $value = 1 ) {
106 return apc_dec( $key, $value );