Completed
Pull Request — development (#2979)
by Stephen
08:55
created

Apc   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 72.09%

Importance

Changes 0
Metric Value
dl 0
loc 112
ccs 31
cts 43
cp 0.7209
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A exists() 0 5 1
A put() 0 19 4
A get() 0 20 3
B clean() 0 13 5
A details() 0 4 2
A isAvailable() 0 4 2
1
<?php
2
3
/**
4
 * This file contains functions that deal with getting and setting cache values.
5
 *
6
 * @name      ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
9
 *
10
 * @version 1.1 Release Candidate 1
11
 *
12
 */
13
14
namespace ElkArte\sources\subs\CacheMethod;
15
16
/**
17
 * Alternative PHP Cache or APC / APCu
18
 */
19
class Apc extends Cache_Method_Abstract
20
{
21
	/**
22
	 * {@inheritdoc}
23
	 */
24
	protected $title = 'Alternative PHP Cache';
25
26
	/**
27
	 * Whether to use the APCu functions or the original APC ones.
28
	 *
29
	 * @var bool
30
	 */
31
	protected $apcu = false;
32
33
	/**
34
	 * {@inheritdoc}
35
	 */
36 2
	public function __construct($options)
37
	{
38 2
		parent::__construct($options);
39 2
		$this->apcu = function_exists('apcu_store');
40 2
	}
41
42
	/**
43
	 * {@inheritdoc}
44
	 */
45 1
	public function exists($key)
46
	{
47 1
		$this->get($key);
48 1
		return !$this->is_miss;
49
	}
50
51
	/**
52
	 * {@inheritdoc}
53
	 */
54 1
	public function put($key, $value, $ttl = 120)
55
	{
56 1
		$prefixedKey = $this->getprefixedKey($key);
57
		// An extended key is needed to counteract a bug in APC.
58 1
		if ($this->apcu)
59 1
		{
60 1
			if ($value === null)
61 1
				apcu_delete($prefixedKey);
62
			else
63 1
				apcu_store($prefixedKey, $value, $ttl);
64 1
		}
65
		else
66
		{
67
			if ($value === null)
68
				apc_delete($prefixedKey);
69
			else
70
				apc_store($prefixedKey, $value, $ttl);
71
		}
72 1
	}
73
74
	/**
75
	 * {@inheritdoc}
76
	 */
77 1
	public function get($key, $ttl = 120)
78
	{
79 1
		$prefixedKey = $this->getprefixedKey($key);
80 1
		$success = false;
81 1
		if ($this->apcu)
82 1
			$result = apcu_fetch($prefixedKey, $success);
83
		else
84
			$result = apc_fetch($prefixedKey, $success);
85 1
		$this->is_miss = !$success;
86
87
		/*
88
		 * Let's be conssistent, yes? All other cache methods
89
		 * supported by ElkArte return null on failure to grab
90
		 * the specified cache entry.
91
		 */
92 1
		if ($this->is_miss)
93 1
			return;
94
95 1
		return $result;
96
	}
97
98
	/**
99
	 * {@inheritdoc}
100
	 */
101 1
	public function clean($type = '')
102
	{
103 1
		if ($this->apcu)
104 1
			apcu_clear_cache();
105
		// If passed a type, clear that type out
106
		elseif ($type === '' || $type === 'data')
107
		{
108
			apc_clear_cache('user');
109
			apc_clear_cache('system');
110
		}
111
		elseif ($type === 'user')
112
			apc_clear_cache('user');
113 1
	}
114
115
	/**
116
	 * {@inheritdoc}
117
	 */
118 2
	public function isAvailable()
119
	{
120 2
		return function_exists('apc_store') || function_exists('apcu_store');
121
	}
122
123
	/**
124
	 * {@inheritdoc}
125
	 */
126
	public function details()
127
	{
128
		return array('title' => $this->title, 'version' => phpversion($this->apcu ? 'apcu' : 'apc'));
129
	}
130
}
131