Test Failed
Push — master ( e6ed6f...83712c )
by Jean-Christophe
11:22
created

ApcuCache::getCacheEntries()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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
		return \apc_exists ( $this->getRealKey ( $key ) );
0 ignored issues
show
Bug introduced by
$this->getRealKey($key) of type string is incompatible with the type boolean|string[] expected by parameter $keys of apc_exists(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
		return \apc_exists ( /** @scrutinizer ignore-type */ $this->getRealKey ( $key ) );
Loading history...
34
	}
35
36
	public function store($key, $code, $tag = null, $php = true) {
37
		$this->storeContent ( $key, $code, $tag );
38
	}
39
40
	/**
41
	 * Caches the given data with the given key.
42
	 *
43
	 * @param string $key cache key
44
	 * @param string $content the source-code to be cached
45
	 * @param string $tag not used
46
	 */
47
	protected function storeContent($key, $content, $tag) {
48
		\apc_store ( $this->getRealKey ( $key ), $content );
49
	}
50
51
	protected function getRealKey($key) {
52
		return $key;
53
	}
54
55
	/**
56
	 * Fetches data stored for the given key.
57
	 *
58
	 * @param string $key cache key
59
	 * @return mixed the cached data
60
	 */
61
	public function fetch($key) {
62
		$result = \apc_fetch ( $this->getRealKey ( $key ) );
63
		return eval ( $result );
64
	}
65
66
	/**
67
	 * return data stored for the given key.
68
	 *
69
	 * @param string $key cache key
70
	 * @return mixed the cached data
71
	 */
72
	public function file_get_contents($key) {
73
		return \apc_fetch ( $this->getRealKey ( $key ) );
74
	}
75
76
	/**
77
	 * Returns the timestamp of the last cache update for the given key.
78
	 *
79
	 * @param string $key cache key
80
	 * @return boolean|int unix timestamp
81
	 */
82
	public function getTimestamp($key) {
83
		$key = $this->getRealKey ( $key );
84
		$cache = \apc_cache_info ();
85
		if (empty ( $cache ['cache_list'] )) {
86
			return false;
87
		}
88
		foreach ( $cache ['cache_list'] as $entry ) {
89
			if ($entry ['info'] != $key) {
90
				continue;
91
			}
92
			$creationTime = $entry ['creation_time'];
93
			return $creationTime;
94
		}
95
		return \time ();
96
	}
97
98
	public function remove($key) {
99
		\apc_delete ( $this->getRealKey ( $key ) );
100
	}
101
102
	public function clear() {
103
		\apc_clear_cache ();
104
	}
105
106
	protected function getCacheEntries($type) {
107
		$entries = $this->getAllEntries ();
108
		return \array_filter ( $entries, function ($v) use ($type) {
109
			return UString::startswith ( $v ['info'], $type );
110
		} );
111
	}
112
113
	protected function getAllEntries() {
114
		$entries = [ ];
115
		$cache = \apc_cache_info ();
116
		if (! empty ( $cache ['cache_list'] )) {
117
			$entries = $cache ['cache_list'];
118
		}
119
		return $entries;
120
	}
121
122
	public function getCacheFiles($type) {
123
		$result = [ ];
124
		$entries = $this->getCacheEntries ( $type );
125
		foreach ( $entries as $entry ) {
126
			$key = $entry ['info'];
127
			if (UString::startswith ( $key, $type )) {
128
				$result [] = new CacheFile ( \ucfirst ( $type ), $key, $entry ['creation_time'], $entry ['mem_size'], $key );
129
			}
130
		}
131
		if (\sizeof ( $result ) === 0)
132
			$result [] = new CacheFile ( \ucfirst ( $type ), "", "", "" );
133
		return $result;
134
	}
135
136
	public function clearCache($type) {
137
		$entries = $this->getCacheEntries ( $type );
138
		foreach ( $entries as $entry ) {
139
			$this->remove ( $entry ['info'] );
140
		}
141
	}
142
143
	public function getEntryKey($key) {
144
		return $this->getRealKey ( $key );
145
	}
146
}
147