Passed
Push — release-2.1 ( f28109...493a4c )
by Mathias
06:40
created

Apcu   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
c 0
b 0
f 0
dl 0
loc 66
rs 10
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A putData() 0 10 3
A getData() 0 7 2
A connect() 0 3 1
A getVersion() 0 3 1
A cleanCache() 0 5 1
A isSupported() 0 8 4
1
<?php
2
3
/**
4
 * Simple Machines Forum (SMF)
5
 *
6
 * @package SMF
7
 * @author Simple Machines https://www.simplemachines.org
8
 * @copyright 2020 Simple Machines and individual contributors
9
 * @license https://www.simplemachines.org/about/smf/license.php BSD
10
 *
11
 * @version 2.1 RC3
12
 */
13
14
namespace SMF\Cache\APIs;
15
16
use SMF\Cache\CacheApi;
17
use SMF\Cache\CacheApiInterface;
18
19
if (!defined('SMF'))
20
	die('No direct access...');
21
22
/**
23
 * Our Cache API class
24
 *
25
 * @package CacheAPI
26
 */
27
class Apcu extends CacheApi implements CacheApiInterface
28
{
29
	/**
30
	 * {@inheritDoc}
31
	 */
32
	public function isSupported($test = false)
33
	{
34
		$supported = function_exists('apcu_fetch') && function_exists('apcu_store');
35
36
		if ($test)
37
			return $supported;
38
39
		return parent::isSupported() && $supported;
40
	}
41
42
	/**
43
	 * {@inheritDoc}
44
	 */
45
	public function connect()
46
	{
47
		return true;
48
	}
49
50
	/**
51
	 * {@inheritDoc}
52
	 */
53
	public function getData($key, $ttl = null)
54
	{
55
		$key = $this->prefix . strtr($key, ':/', '-_');
56
57
		$value = apcu_fetch($key . 'smf');
58
59
		return !empty($value) ? $value : null;
60
	}
61
62
	/**
63
	 * {@inheritDoc}
64
	 */
65
	public function putData($key, $value, $ttl = null)
66
	{
67
		$key = $this->prefix . strtr($key, ':/', '-_');
68
69
		// An extended key is needed to counteract a bug in APC.
70
		if ($value === null)
71
			return apcu_delete($key . 'smf');
72
73
		else
74
			return apcu_store($key . 'smf', $value, $ttl !== null ? $ttl : $this->ttl);
75
	}
76
77
	/**
78
	 * {@inheritDoc}
79
	 */
80
	public function cleanCache($type = '')
81
	{
82
		$this->invalidateCache();
83
84
		return apcu_clear_cache();
85
	}
86
87
	/**
88
	 * {@inheritDoc}
89
	 */
90
	public function getVersion()
91
	{
92
		return phpversion('apcu');
93
	}
94
}
95
96
?>