|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Soupmix\Cache; |
|
4
|
|
|
|
|
5
|
|
|
use Redis; |
|
6
|
|
|
|
|
7
|
|
|
class RedisCache implements CacheInterface |
|
8
|
|
|
{ |
|
9
|
|
|
private $handler = null; |
|
10
|
|
|
|
|
11
|
|
|
private $serializer = "PHP"; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Connect to Redis service |
|
15
|
|
|
* |
|
16
|
|
|
* @param Redis $handler Configuration values that has dbIndex name and host's IP address |
|
17
|
|
|
* |
|
18
|
|
|
*/ |
|
19
|
8 |
|
public function __construct(Redis $handler) |
|
20
|
|
|
{ |
|
21
|
8 |
|
$this->handler = $handler; |
|
22
|
8 |
|
if (function_exists('igbinary_serialize')) { |
|
23
|
|
|
$this->serializer = "IGBINARY"; |
|
24
|
|
|
} |
|
25
|
8 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
public function getConnection() |
|
29
|
|
|
{ |
|
30
|
|
|
return $this->handler; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Fetch a value from the cache. |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $key The unique key of this item in the cache |
|
38
|
|
|
* |
|
39
|
|
|
* @return mixed The value of the item from the cache, or null in case of cache miss |
|
40
|
|
|
*/ |
|
41
|
2 |
|
public function get($key) |
|
42
|
|
|
{ |
|
43
|
2 |
|
$value = $this->handler->get($key); |
|
44
|
2 |
|
return ($value) ? $this->unserialize($value) : null; |
|
45
|
|
|
} |
|
46
|
|
|
/** |
|
47
|
|
|
* Persist data in the cache, uniquely referenced by a key with an optional expiration TTL time. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $key The key of the item to store |
|
50
|
|
|
* @param mixed $value The value of the item to store |
|
51
|
|
|
* @param null|integer|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and the driver supports TTL |
|
52
|
|
|
* then the library may set a default value for it or let the driver take care of that. |
|
53
|
|
|
* |
|
54
|
|
|
* @return bool True on success and false on failure |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public function set($key, $value, $ttl = null) |
|
57
|
|
|
{ |
|
58
|
2 |
|
$ttl = intval($ttl); |
|
59
|
2 |
|
$value = $this->serialize($value); |
|
60
|
2 |
|
if($ttl ==0 ){ |
|
61
|
2 |
|
return $this->handler->set($key, $value); |
|
62
|
|
|
} |
|
63
|
|
|
return $this->handler->set($key, $value, $ttl); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
2 |
|
private function serialize($value) |
|
67
|
|
|
{ |
|
68
|
2 |
|
return ($this->serializer === "IGBINARY") ? igbinary_serialize($value) : serialize($value); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
2 |
|
private function unserialize($value) |
|
72
|
|
|
{ |
|
73
|
2 |
|
return ($this->serializer === "IGBINARY") ? igbinary_unserialize($value) : unserialize($value); |
|
74
|
|
|
} |
|
75
|
|
|
/** |
|
76
|
|
|
* Delete an item from the cache by its unique key |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $key The unique cache key of the item to delete |
|
79
|
|
|
* |
|
80
|
|
|
* @return bool True on success and false on failure |
|
81
|
|
|
*/ |
|
82
|
4 |
|
public function delete($key) |
|
83
|
|
|
{ |
|
84
|
4 |
|
return (bool) $this->handler->delete($key); |
|
85
|
|
|
} |
|
86
|
|
|
/** |
|
87
|
|
|
* Wipe clean the entire cache's keys |
|
88
|
|
|
* |
|
89
|
|
|
* @return bool True on success and false on failure |
|
90
|
|
|
*/ |
|
91
|
8 |
|
public function clear() |
|
92
|
|
|
{ |
|
93
|
8 |
|
return $this->handler->flushDb(); |
|
94
|
|
|
} |
|
95
|
|
|
/** |
|
96
|
|
|
* Obtain multiple cache items by their unique keys |
|
97
|
|
|
* |
|
98
|
|
|
* @param array|Traversable $keys A list of keys that can obtained in a single operation. |
|
99
|
|
|
* |
|
100
|
|
|
* @return array An array of key => value pairs. Cache keys that do not exist or are stale will have a value of null. |
|
101
|
|
|
*/ |
|
102
|
2 |
|
public function getMultiple($keys) |
|
103
|
|
|
{ |
|
104
|
2 |
|
return array_combine($keys, $this->handler->mGet($keys)); |
|
105
|
|
|
} |
|
106
|
|
|
/** |
|
107
|
|
|
* Persisting a set of key => value pairs in the cache, with an optional TTL. |
|
108
|
|
|
* |
|
109
|
|
|
* @param array|Traversable $items An array of key => value pairs for a multiple-set operation. |
|
110
|
|
|
* @param null|integer|DateInterval $ttl Optional. The amount of seconds from the current time that the item will exist in the cache for. |
|
111
|
|
|
* If this is null then the cache backend will fall back to its own default behaviour. |
|
112
|
|
|
* |
|
113
|
|
|
* @return bool True on success and false on failure |
|
114
|
|
|
*/ |
|
115
|
2 |
|
public function setMultiple($items, $ttl = null) |
|
116
|
|
|
{ |
|
117
|
2 |
|
if (($ttl === null) || ($ttl === 0)) { |
|
118
|
2 |
|
return $this->handler->mSet($items); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$return =[]; |
|
122
|
|
|
foreach ($items as $key=>$value) { |
|
123
|
|
|
$return[$key] = $this->set($key, $value, $ttl); |
|
124
|
|
|
} |
|
125
|
|
|
return $return; |
|
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
/** |
|
128
|
|
|
* Delete multiple cache items in a single operation |
|
129
|
|
|
* |
|
130
|
|
|
* @param array|Traversable $keys The array of string-based keys to be deleted |
|
131
|
|
|
* |
|
132
|
|
|
* @return bool True on success and false on failure |
|
133
|
|
|
*/ |
|
134
|
2 |
|
public function deleteMultiple($keys) |
|
135
|
|
|
{ |
|
136
|
2 |
|
$return =[]; |
|
137
|
2 |
|
foreach ($keys as $key) { |
|
138
|
2 |
|
$return[$key] = (bool) $this->delete($key); |
|
139
|
2 |
|
} |
|
140
|
2 |
|
return $return; |
|
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
/** |
|
143
|
|
|
* Increment a value atomically in the cache by its step value, which defaults to 1 |
|
144
|
|
|
* |
|
145
|
|
|
* @param string $key The cache item key |
|
146
|
|
|
* @param integer $step The value to increment by, defaulting to 1 |
|
147
|
|
|
* |
|
148
|
|
|
* @return int|bool The new value on success and false on failure |
|
149
|
|
|
*/ |
|
150
|
2 |
|
public function increment($key, $step = 1) |
|
151
|
|
|
{ |
|
152
|
2 |
|
return $this->handler->incr($key, $step); |
|
153
|
|
|
} |
|
154
|
|
|
/** |
|
155
|
|
|
* Decrement a value atomically in the cache by its step value, which defaults to 1 |
|
156
|
|
|
* |
|
157
|
|
|
* @param string $key The cache item key |
|
158
|
|
|
* @param integer $step The value to decrement by, defaulting to 1 |
|
159
|
|
|
* |
|
160
|
|
|
* @return int|bool The new value on success and false on failure |
|
161
|
|
|
*/ |
|
162
|
2 |
|
public function decrement($key, $step = 1) |
|
163
|
|
|
{ |
|
164
|
2 |
|
return $this->handler->decr($key, $step); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.