Test Failed
Pull Request — master (#97)
by Gildonei
03:38
created

MemCachedDriver::getCacheFiles()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 9
c 2
b 0
f 0
dl 0
loc 13
ccs 0
cts 12
cp 0
rs 9.9666
cc 4
nc 6
nop 1
crap 20
1
<?php
2
3
namespace Ubiquity\cache\system;
4
5
use Ubiquity\cache\CacheFile;
6
7
/**
8
 * This class is responsible for storing values with MemCached.
9
 * Ubiquity\cache\system$MemCachedDriver
10
 * This class is part of Ubiquity
11
 *
12
 * @author jcheron <[email protected]>
13
 * @version 1.0.0
14
 *
15
 */
16
class MemCachedDriver extends AbstractDataCache {
17
	/**
18
	 *
19
	 * @var \Memcached
20
	 */
21
	private $cacheInstance;
22
	private const CONTENT = 'content';
23
	private const TAG = 'tag';
24
	private const TIME = 'time';
25
26
	/**
27
	 * Initializes the cache-provider
28
	 */
29
	public function __construct($root, $postfix = "", $cacheParams = []) {
30
		parent::__construct ( $root, $postfix );
31
		$defaultParams = [ 'server' => '127.0.0.1','port' => 11211 ];
32
		$cacheParams = \array_merge ( $cacheParams, $defaultParams );
33
		$this->cacheInstance = new \Memcached ();
34
		$this->cacheInstance->addServer ( $cacheParams ['server'], $cacheParams ['port'] );
35
	}
36
37
	/**
38
	 * Check if annotation-data for the key has been stored.
39
	 *
40
	 * @param string $key cache key
41
	 * @return boolean true if data with the given key has been stored; otherwise false
42
	 */
43
	public function exists($key) {
44
		$k = $this->getRealKey ( $key );
45
		$this->cacheInstance->get ( $k );
46
		return \Memcached::RES_NOTFOUND !== $this->cacheInstance->getResultCode ();
47
	}
48
49
	public function store($key, $code, $tag = null, $php = true) {
50
		$this->storeContent ( $key, $code, $tag );
51
	}
52
53
	/**
54
	 * Caches the given data with the given key.
55
	 *
56
	 * @param string $key cache key
57
	 * @param string $content the source-code to be cached
58
	 * @param string $tag
59
	 */
60
	protected function storeContent($key, $content, $tag) {
61
		$key = $this->getRealKey ( $key );
62
		$this->cacheInstance->set ( $key, [ self::CONTENT => $content,self::TAG => $tag,self::TIME => \time () ] );
63
	}
64
65
	protected function getRealKey($key) {
66
		$key = \str_replace ( "/", "-", $key );
67
		return \str_replace ( "\\", "-", $key );
68
	}
69
70
	/**
71
	 * Fetches data stored for the given key.
72
	 *
73
	 * @param string $key cache key
74
	 * @return mixed the cached data
75
	 */
76
	public function fetch($key) {
77
		$result = $this->cacheInstance->get ( $this->getRealKey ( $key ) ) [self::CONTENT];
78
		return eval ( $result );
79
	}
80
81
	/**
82
	 * return data stored for the given key.
83
	 *
84
	 * @param string $key cache key
85
	 * @return mixed the cached data
86
	 */
87
	public function file_get_contents($key) {
88
		return $this->cacheInstance->get ( $this->getRealKey ( $key ) ) [self::CONTENT];
89
	}
90
91
	/**
92
	 * Returns the timestamp of the last cache update for the given key.
93
	 *
94
	 * @param string $key cache key
95
	 * @return int unix timestamp
96
	 */
97
	public function getTimestamp($key) {
98
		$key = $this->getRealKey ( $key );
99
		return $this->cacheInstance->get ( $key ) [self::TIME];
100
	}
101
102
	public function remove($key) {
103
		$key = $this->getRealKey ( $key );
104
		$this->cacheInstance->delete ( $this->getRealKey ( $key ) );
105
	}
106
107
	public function clear() {
108
		$this->cacheInstance->flush ();
109
	}
110
111
	public function getCacheFiles($type) {
112
		$result = [ ];
113
		$keys = $this->cacheInstance->getAllKeys ();
114
115
		foreach ( $keys as $key ) {
116
			$entry = $this->cacheInstance->get ( $key );
117
			if ($entry [self::TAG] === $type) {
118
				$result [] = new CacheFile ( \ucfirst ( $type ), $key, $entry [self::TIME], "", $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
		$keys = $this->cacheInstance->getAllKeys ();
128
		foreach ( $keys as $key ) {
129
			$entry = $this->cacheInstance->get ( $key );
130
			if ($entry [self::TAG] === $type) {
131
				$this->cacheInstance->delete ( $key );
132
			}
133
		}
134
	}
135
136
	public function getCacheInfo() {
137
		return parent::getCacheInfo () . "<br>Driver name : <b>" . \Memcached::class . "</b>";
138
	}
139
140
	public function getEntryKey($key) {
141
		return $this->getRealKey ( $key );
142
	}
143
}
144