Completed
Pull Request — stable8.2 (#24508)
by Joas
11:24
created

apc.php ➔ apc_exists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 9.4285
1
<?php
2
/**
3
 * @author Andreas Fischer <[email protected]>
4
 * @author Morris Jobke <[email protected]>
5
 * @author Otto Sabart <[email protected]>
6
 * @author Robin Appelman <[email protected]>
7
 *
8
 * @copyright Copyright (c) 2015, ownCloud, Inc.
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OC\Memcache;
26
27
use OCP\IMemcache;
28
29
class APC extends Cache implements IMemcache {
30
	use CASTrait {
31
		cas as casEmulated;
32
	}
33
34
	use CADTrait;
35
36
	public function get($key) {
37
		$result = apc_fetch($this->getPrefix() . $key, $success);
38
		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...
39
			return null;
40
		}
41
		return $result;
42
	}
43
44
	public function set($key, $value, $ttl = 0) {
45
		return apc_store($this->getPrefix() . $key, $value, $ttl);
46
	}
47
48
	public function hasKey($key) {
49
		return apc_exists($this->getPrefix() . $key);
50
	}
51
52
	public function remove($key) {
53
		return apc_delete($this->getPrefix() . $key);
54
	}
55
56
	public function clear($prefix = '') {
57
		$ns = $this->getPrefix() . $prefix;
58
		$ns = preg_quote($ns, '/');
59
		$iter = new \APCIterator('user', '/^' . $ns . '/', APC_ITER_KEY);
60
		return apc_delete($iter);
61
	}
62
63
	/**
64
	 * Set a value in the cache if it's not already stored
65
	 *
66
	 * @param string $key
67
	 * @param mixed $value
68
	 * @param int $ttl Time To Live in seconds. Defaults to 60*60*24
69
	 * @return bool
70
	 */
71
	public function add($key, $value, $ttl = 0) {
72
		return apc_add($this->getPrefix() . $key, $value, $ttl);
73
	}
74
75
	/**
76
	 * Increase a stored number
77
	 *
78
	 * @param string $key
79
	 * @param int $step
80
	 * @return int | bool
81
	 */
82
	public function inc($key, $step = 1) {
83
		$this->add($key, 0);
84
		return apc_inc($this->getPrefix() . $key, $step);
85
	}
86
87
	/**
88
	 * Decrease a stored number
89
	 *
90
	 * @param string $key
91
	 * @param int $step
92
	 * @return int | bool
93
	 */
94
	public function dec($key, $step = 1) {
95
		return apc_dec($this->getPrefix() . $key, $step);
96
	}
97
98
	/**
99
	 * Compare and set
100
	 *
101
	 * @param string $key
102
	 * @param mixed $old
103
	 * @param mixed $new
104
	 * @return bool
105
	 */
106
	public function cas($key, $old, $new) {
107
		// apc only does cas for ints
108
		if (is_int($old) and is_int($new)) {
109
			return apc_cas($this->getPrefix() . $key, $old, $new);
110
		} else {
111
			return $this->casEmulated($key, $old, $new);
112
		}
113
	}
114
115
	static public function isAvailable() {
116
		if (!extension_loaded('apc')) {
117
			return false;
118
		} elseif (!ini_get('apc.enabled')) {
119
			return false;
120
		} elseif (!ini_get('apc.enable_cli') && \OC::$CLI) {
121
			return false;
122
		} else {
123
			return true;
124
		}
125
	}
126
}
127
128
if (!function_exists('apc_exists')) {
129
	function apc_exists($keys) {
130
		$result = false;
131
		apc_fetch($keys, $result);
132
		return $result;
133
	}
134
}
135