CacheAPC::clean()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Cachearium\Backend;
4
5
use Cachearium\CacheAbstract;
6
7
/**
8
 * Caches in APC
9
 *
10
 */
11
class CacheAPC extends CacheAbstract {
12
	// @codeCoverageIgnoreStart
13
	/**
14
	 * Cache constructor (this is a singleton).
15
	 *
16
	 * @return Cache The cache singleton.
17
	 */
18
	static public function singleton() {
19
		static $instances;
20
21
		if (!isset($instances)) {
22
			$instances = new CacheAPC();
23
		}
24
		return $instances;
25
	}
26
27
	// Prevent users to clone the instance
28
	public function __clone() {
29
		trigger_error('Cloning is not allowed.', LH_TRIGGER_UNEXPECTED);
30
	}
31
	// @codeCoverageIgnoreEnd
32
33
	/**
34
	 * Constructor.
35
	 * @codeCoverageIgnore
36
	*/
37
	private function __construct() {
38
		$this->enable();
39
	}
40
41
	public function enable() {
42
		if (!extension_loaded('apc')) {
43
			$this->enabled = false;
44
			return false;
45
		}
46
		return parent::enable();
47
	}
48
49
	private function checkValidArgs($base, $id, $sub) {
50
		if (is_array($base) || is_array($id) || !is_string($sub)) {
51
			throw new CacheInvalidDataException('Invalid get parameter');
52
		}
53
	}
54
55
	public function get($base, $id, $sub = LH_DEFAULT_CACHE_ID) {
56
		// @codeCoverageIgnoreStart
57
		if (!$this->enabled) {
58
			throw new NotCachedException();
59
		}
60
		// @codeCoverageIgnoreEnd
61
62
		if (!is_string($sub)) {
63
			$sub = md5(serialize($sub));
64
		}
65
		$this->checkValidArgs($base, $id, $sub);
66
67
		$key = (new CacheKey($base, $id, $sub))->getHash();
68
		$success = false;
69
		$data = apc_fetch($key, $success);
70
		if (!$success) {
71
			$this->log(CacheLogEnum::MISSED, $base, $id, $sub);
72
			throw new NotCachedException();
73
		}
74
		return $data;
75
	}
76
77
	public function store($data, $base, $id, $sub = LH_DEFAULT_CACHE_ID, $lifetime = 0) {
78
		// @codeCoverageIgnoreStart
79
		if (!$this->enabled) {
80
			return false;
81
		}
82
		// @codeCoverageIgnoreEnd
83
84
		if (!is_string($sub)) {
85
			$sub = md5(serialize($sub));
86
		}
87
		$this->checkValidArgs($base, $id, $sub);
88
89
		$key = (new CacheKey($base, $id, $sub))->getHash();
90
		apc_store($key, $data, $lifetime);
91
		return true;
92
	}
93
94
	public function delete($base, $id, $sub = LH_DEFAULT_CACHE_ID) {
95
		if (!is_string($sub)) {
96
			$sub = md5(serialize($sub));
97
		}
98
99
		$this->checkValidArgs($base, $id, $sub);
100
101
		$key = (new CacheKey($base, $id, $sub))->getHash();
102
		apc_delete($key);
103
		return true;
104
	}
105
106
	public function clean($base, $id) {
107
		// TODO
108
		return true;
109
	}
110
111
	public function clear() {
112
		apc_clear_cache('user');
113
		return true;
114
	}
115
116
	public function prefetch($data) {
117
		// nothing.
118
	}
119
120
	public function report() {
121
		if ($this->should_log == false) {
122
			return;
123
		}
124
		// TODO
125
	}
126
}
127