Passed
Push — master ( fd0d63...16794a )
by smiley
01:53
created

RedisCache::setMultiple()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 17
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
/**
3
 * Class RedisCache
4
 *
5
 * @filesource   RedisCache.php
6
 * @created      27.05.2017
7
 * @package      chillerlan\SimpleCache
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\SimpleCache;
14
15
use chillerlan\Settings\SettingsContainerInterface;
16
use Redis;
17
18
class RedisCache extends CacheDriverAbstract{
19
20
	/**
21
	 * @var \Redis
22
	 */
23
	protected $redis;
24
25
	/**
26
	 * RedisCache constructor.
27
	 *
28
	 * @param \Redis                                             $redis
29
	 * @param \chillerlan\Settings\SettingsContainerInterface|null $options
30
	 */
31
	public function __construct(Redis $redis, SettingsContainerInterface $options = null){
32
		parent::__construct($options);
33
34
		$this->redis = $redis;
35
	}
36
37
	/** @inheritdoc */
38
	public function get($key, $default = null){
39
		$this->checkKey($key);
40
41
		$value = $this->redis->get($key);
42
43
		if($value !== false){
44
			return $value;
45
		}
46
47
		return $default;
48
	}
49
50
	/** @inheritdoc */
51
	public function set($key, $value, $ttl = null):bool{
52
		$this->checkKey($key);
53
54
		$ttl = $this->getTTL($ttl);
55
56
		if($ttl === null){
0 ignored issues
show
introduced by
The condition $ttl === null is always false.
Loading history...
57
			return $this->redis->set($key, $value);
58
		}
59
60
		return $this->redis->setex($key, $ttl, $value);
61
	}
62
63
	/** @inheritdoc */
64
	public function delete($key):bool{
65
		$this->checkKey($key);
66
67
		return (bool)$this->redis->delete($key);
68
	}
69
70
	/** @inheritdoc */
71
	public function clear():bool{
72
		return $this->redis->flushDB();
73
	}
74
75
	/** @inheritdoc */
76
	public function getMultiple($keys, $default = null):array{
77
		$keys = $this->getData($keys);
78
79
		$this->checkKeyArray($keys);
80
81
		// scary
82
		$values = array_combine($keys, $this->redis->mget($keys));
83
84
		$return = [];
85
86
		foreach($keys as $key){
87
			$return[$key] = $values[$key] !== false ? $values[$key] : $default;
88
		}
89
90
		return $return;
91
	}
92
93
	/** @inheritdoc */
94
	public function setMultiple($values, $ttl = null):bool{
95
		$values = $this->getData($values);
96
		$ttl    = $this->getTTL($ttl);
97
98
		if($ttl === null){
0 ignored issues
show
introduced by
The condition $ttl === null is always false.
Loading history...
99
			$this->checkKeyArray(array_keys($values));
100
101
			return $this->redis->msetnx($values);
102
		}
103
104
		$return = [];
105
106
		foreach($values as $key => $value){
107
			$return[] = $this->set($key, $value, $ttl);
108
		}
109
110
		return $this->checkReturn($return);
111
	}
112
113
	/** @inheritdoc */
114
	public function deleteMultiple($keys):bool{
115
		$keys = $this->getData($keys);
116
117
		$this->checkKeyArray($keys);
118
119
		return (bool)$this->redis->delete($keys);
120
	}
121
122
}
123