Passed
Push — master ( 118cad...297df4 )
by Jean-Christophe
10:04
created

ApcuCache::clearCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Ubiquity\cache\system;
4
5
use Ubiquity\utils\base\UString;
6
use Ubiquity\cache\CacheFile;
7
8
/**
9
 * APC cache implementation
10
 * Ubiquity\cache\system$ApcCache
11
 * This class is part of Ubiquity
12
 *
13
 * @author jcheron <[email protected]>
14
 * @version 1.0.2
15
 *
16
 */
17
class ApcuCache extends AbstractDataCache {
18
19
	/**
20
	 * Initializes the apc cache-provider
21
	 */
22
	public function __construct($root, $postfix = "") {
23
		parent::__construct ( $root, $postfix );
24
	}
25
26
	/**
27
	 * Check if annotation-data for the key has been stored.
28
	 *
29
	 * @param string $key cache key
30
	 * @return string[]|boolean true if data with the given key has been stored; otherwise false
31
	 */
32
	public function exists($key) {
33
		$success = false;
34
		\apc_fetch ( $this->getRealKey ( $key ), $success );
35
		return $success;
36
	}
37
38
	public function store($key, $code, $tag = null) {
39
		\apc_store ( $this->getRealKey ( $key ), $code );
40
	}
41
42
	protected function getRealKey($key) {
43
		return $key;
44
	}
45
46
	/**
47
	 * Fetches data stored for the given key.
48
	 *
49
	 * @param string $key cache key
50
	 * @return mixed the cached data
51
	 */
52
	public function fetch($key) {
53
		return \apc_fetch ( $this->getRealKey ( $key ) );
54
	}
55
56
	/**
57
	 * return data stored for the given key.
58
	 *
59
	 * @param string $key cache key
60
	 * @return mixed the cached data
61
	 */
62
	public function file_get_contents($key) {
63
		return \apc_fetch ( $this->getRealKey ( $key ) );
64
	}
65
66
	/**
67
	 * Returns the timestamp of the last cache update for the given key.
68
	 *
69
	 * @param string $key cache key
70
	 * @return boolean|int unix timestamp
71
	 */
72
	public function getTimestamp($key) {
73
		$key = $this->getRealKey ( $key );
74
		$cache = \apc_cache_info ( 'user' );
75
		if (empty ( $cache ['cache_list'] )) {
76
			return false;
77
		}
78
		foreach ( $cache ['cache_list'] as $entry ) {
79
			if ($entry ['info'] != $key) {
80
				continue;
81
			}
82
			$creationTime = $entry ['creation_time'];
83
			return $creationTime;
84
		}
85
		return \time ();
86
	}
87
88
	public function remove($key) {
89
		\apc_delete ( $this->getRealKey ( $key ) );
90
	}
91
92
	public function clear() {
93
		\apc_clear_cache ( 'user' );
94
	}
95
96
	protected function getCacheEntries($type) {
97
		$entries = $this->getAllEntries ();
98
		return \array_filter ( $entries, function ($v) use ($type) {
99
			return UString::startswith ( $v ['info'], $type );
100
		} );
101
	}
102
103
	protected function getAllEntries() {
104
		$entries = [ ];
105
		$cache = \apc_cache_info ( 'user' );
106
		if (! empty ( $cache ['cache_list'] )) {
107
			$entries = $cache ['cache_list'];
108
		}
109
		return $entries;
110
	}
111
112
	public function getCacheFiles($type) {
113
		$result = [ ];
114
		$entries = $this->getCacheEntries ( $type );
115
		foreach ( $entries as $entry ) {
116
			$key = $entry ['info'];
117
			if (UString::startswith ( $key, $type )) {
118
				$result [] = new CacheFile ( \ucfirst ( $type ), $key, $entry ['creation_time'], $entry ['mem_size'], $key );
119
			}
120
		}
121
		if (\sizeof ( $result ) === 0)
122
			$result [] = new CacheFile ( \ucfirst ( $type ), "", "", "" );
123
		return $result;
124
	}
125
126
	public function clearCache($type) {
127
		$entries = $this->getCacheEntries ( $type );
128
		foreach ( $entries as $entry ) {
129
			$this->remove ( $entry ['info'] );
130
		}
131
	}
132
133
	public function getEntryKey($key) {
134
		return $this->getRealKey ( $key );
135
	}
136
}
137