Completed
Push — master ( 4b2544...2fd4f1 )
by Blizzz
09:28
created

APC.php ➔ apc_exists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 5
rs 9.4285
1
<?php
2
/**
3
 * @author Andreas Fischer <[email protected]>
4
 * @author Clark Tomlinson <[email protected]>
5
 * @author Morris Jobke <[email protected]>
6
 * @author Otto Sabart <[email protected]>
7
 * @author Robin Appelman <[email protected]>
8
 *
9
 * @copyright Copyright (c) 2016, ownCloud, Inc.
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
26
namespace OC\Memcache;
27
28
use OCP\IMemcache;
29
30
class APC extends Cache implements IMemcache {
31
	use CASTrait {
32
		cas as casEmulated;
33
	}
34
35
	use CADTrait;
36
37
	public function get($key) {
38
		$result = apc_fetch($this->getPrefix() . $key, $success);
39
		if (!$success) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $success of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
40
			return null;
41
		}
42
		return $result;
43
	}
44
45
	public function set($key, $value, $ttl = 0) {
46
		return apc_store($this->getPrefix() . $key, $value, $ttl);
47
	}
48
49
	public function hasKey($key) {
50
		return apc_exists($this->getPrefix() . $key);
51
	}
52
53
	public function remove($key) {
54
		return apc_delete($this->getPrefix() . $key);
55
	}
56
57
	public function clear($prefix = '') {
58
		$ns = $this->getPrefix() . $prefix;
59
		$ns = preg_quote($ns, '/');
60
		$iter = new \APCIterator('user', '/^' . $ns . '/', APC_ITER_KEY);
61
		return apc_delete($iter);
62
	}
63
64
	/**
65
	 * Set a value in the cache if it's not already stored
66
	 *
67
	 * @param string $key
68
	 * @param mixed $value
69
	 * @param int $ttl Time To Live in seconds. Defaults to 60*60*24
70
	 * @return bool
71
	 */
72
	public function add($key, $value, $ttl = 0) {
73
		return apc_add($this->getPrefix() . $key, $value, $ttl);
74
	}
75
76
	/**
77
	 * Increase a stored number
78
	 *
79
	 * @param string $key
80
	 * @param int $step
81
	 * @return int | bool
82
	 */
83
	public function inc($key, $step = 1) {
84
		$this->add($key, 0);
85
		return apc_inc($this->getPrefix() . $key, $step);
86
	}
87
88
	/**
89
	 * Decrease a stored number
90
	 *
91
	 * @param string $key
92
	 * @param int $step
93
	 * @return int | bool
94
	 */
95
	public function dec($key, $step = 1) {
96
		return apc_dec($this->getPrefix() . $key, $step);
97
	}
98
99
	/**
100
	 * Compare and set
101
	 *
102
	 * @param string $key
103
	 * @param mixed $old
104
	 * @param mixed $new
105
	 * @return bool
106
	 */
107 View Code Duplication
	public function cas($key, $old, $new) {
108
		// apc only does cas for ints
109
		if (is_int($old) and is_int($new)) {
110
			return apc_cas($this->getPrefix() . $key, $old, $new);
111
		} else {
112
			return $this->casEmulated($key, $old, $new);
113
		}
114
	}
115
116
	static public function isAvailable() {
117
		if (!extension_loaded('apc')) {
118
			return false;
119
		} elseif (!\OC::$server->getIniWrapper()->getBool('apc.enabled')) {
120
			return false;
121
		} elseif (!\OC::$server->getIniWrapper()->getBool('apc.enable_cli') && \OC::$CLI) {
122
			return false;
123
		} else {
124
			return true;
125
		}
126
	}
127
}
128
129
if (!function_exists('apc_exists')) {
130
	function apc_exists($keys) {
131
		$result = false;
132
		apc_fetch($keys, $result);
133
		return $result;
134
	}
135
}
136