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

RedisCacheDriver::file_get_contents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 2
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
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 Redis.
9
 * Ubiquity\cache\system$RedisCacheDriver
10
 * This class is part of Ubiquity
11
 *
12
 * @author jcheron <[email protected]>
13
 * @version 1.0.1
14
 *
15
 */
16
class RedisCacheDriver extends AbstractDataCache {
17
	/**
18
	 *
19
	 * @var \Redis
20
	 */
21
	private $cacheInstance;
22
23
	/**
24
	 * Initializes the cache-provider
25
	 */
26
	public function __construct($root, $postfix = "", $cacheParams = [ ]) {
27
		parent::__construct ( $root, $postfix );
28
		$defaultParams = [ 'server' => '0.0.0.0','port' => 6379,'persistent' => true,'serializer' => \Redis::SERIALIZER_PHP ];
29
		$cacheParams = \array_merge ( $defaultParams, $cacheParams );
30
		$this->cacheInstance = new \Redis ();
31
		$connect = 'connect';
32
		if ($cacheParams ['persistent'] ?? true) {
33
			$connect = 'pconnect';
34
		}
35
		$this->cacheInstance->{$connect} ( $cacheParams ['server'], $cacheParams ['port'] );
36
		if (isset ( $cacheParams ['serializer'] )) {
37
			$this->cacheInstance->setOption ( \Redis::OPT_SERIALIZER, $cacheParams ['serializer'] );
38
		}
39
	}
40
41
	public function setSerializer($serializer) {
42
		$this->cacheInstance->setOption ( \Redis::OPT_SERIALIZER, $serializer );
43
	}
44
45
	/**
46
	 * Check if annotation-data for the key has been stored.
47
	 *
48
	 * @param string $key cache key
49
	 * @return boolean true if data with the given key has been stored; otherwise false
50
	 */
51
	public function exists($key) {
52
		return $this->cacheInstance->exists ( $this->getRealKey ( $key ) );
53
	}
54
55
	public function store($key, $content, $tag = null) {
56
		$this->cacheInstance->set ( $this->getRealKey ( $key ), $content );
57
	}
58
59
	protected function getRealKey($key) {
60
		return \str_replace ( [ '/','\\' ], "-", $key );
61
	}
62
63
	/**
64
	 * Fetches data stored for the given key.
65
	 *
66
	 * @param string $key cache key
67
	 * @return mixed the cached data
68
	 */
69
	public function fetch($key) {
70
		return $this->cacheInstance->get ( $this->getRealKey ( $key ) );
71
	}
72
73
	/**
74
	 * return data stored for the given key.
75
	 *
76
	 * @param string $key cache key
77
	 * @return mixed the cached data
78
	 */
79
	public function file_get_contents($key) {
80
		return $this->cacheInstance->get ( $this->getRealKey ( $key ) );
81
	}
82
83
	/**
84
	 * Returns the timestamp of the last cache update for the given key.
85
	 *
86
	 * @param string $key cache key
87
	 * @return int unix timestamp
88
	 */
89
	public function getTimestamp($key) {
90
		return $this->cacheInstance->ttl ( $this->getRealKey ( $key ) );
91
	}
92
93
	public function remove($key) {
94
		$this->cacheInstance->delete ( $this->getRealKey ( $key ) );
95
	}
96
97
	public function clear() {
98
		$this->cacheInstance->flushAll ();
99
	}
100
101
	public function getCacheFiles($type) {
102
		$result = [ ];
103
		$keys = $this->cacheInstance->keys ( $type );
104
105
		foreach ( $keys as $key ) {
106
			$ttl = $this->cacheInstance->ttl ( $key );
107
			$result [] = new CacheFile ( \ucfirst ( $type ), $key, $ttl, "", $key );
108
		}
109
		if (\sizeof ( $result ) === 0)
110
			$result [] = new CacheFile ( \ucfirst ( $type ), "", "", "" );
111
		return $result;
112
	}
113
114
	public function clearCache($type) {
115
		$keys = $this->cacheInstance->keys ( $type );
116
		foreach ( $keys as $key ) {
117
			$this->cacheInstance->delete ( $key );
118
		}
119
	}
120
121
	public function getCacheInfo() {
122
		return parent::getCacheInfo () . "<br>Driver name : <b>" . \Redis::class . "</b>";
123
	}
124
125
	public function getEntryKey($key) {
126
		return $this->getRealKey ( $key );
127
	}
128
}
129