1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Joas Schilling <[email protected]> |
4
|
|
|
* @author Jörn Friedrich Dreyer <[email protected]> |
5
|
|
|
* @author Lukas Reschke <[email protected]> |
6
|
|
|
* @author Michael Telatynski <[email protected]> |
7
|
|
|
* @author Morris Jobke <[email protected]> |
8
|
|
|
* @author Robin Appelman <[email protected]> |
9
|
|
|
* |
10
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
11
|
|
|
* @license AGPL-3.0 |
12
|
|
|
* |
13
|
|
|
* This code is free software: you can redistribute it and/or modify |
14
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
15
|
|
|
* as published by the Free Software Foundation. |
16
|
|
|
* |
17
|
|
|
* This program is distributed in the hope that it will be useful, |
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20
|
|
|
* GNU Affero General Public License for more details. |
21
|
|
|
* |
22
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
23
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace OC\Memcache; |
28
|
|
|
|
29
|
|
|
use OCP\IMemcacheTTL; |
30
|
|
|
|
31
|
|
|
class Redis extends Cache implements IMemcacheTTL { |
32
|
|
|
/** |
33
|
|
|
* @var \Redis $cache |
34
|
|
|
*/ |
35
|
|
|
private static $cache = null; |
36
|
|
|
|
37
|
|
|
public function __construct($prefix = '') { |
38
|
|
|
parent::__construct($prefix); |
39
|
|
|
if (is_null(self::$cache)) { |
40
|
|
|
self::$cache = \OC::$server->getGetRedisFactory()->getInstance(); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* entries in redis get namespaced to prevent collisions between ownCloud instances and users |
46
|
|
|
*/ |
47
|
|
|
protected function getNameSpace() { |
48
|
|
|
return $this->prefix; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function get($key) { |
52
|
|
|
$result = self::$cache->get($this->getNamespace() . $key); |
53
|
|
|
if ($result === false && !self::$cache->exists($this->getNamespace() . $key)) { |
54
|
|
|
return null; |
55
|
|
|
} else { |
56
|
|
|
return json_decode($result, true); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function set($key, $value, $ttl = 0) { |
61
|
|
|
if ($ttl > 0) { |
62
|
|
|
return self::$cache->setex($this->getNamespace() . $key, $ttl, json_encode($value)); |
63
|
|
|
} else { |
64
|
|
|
return self::$cache->set($this->getNamespace() . $key, json_encode($value)); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function hasKey($key) { |
69
|
|
|
return self::$cache->exists($this->getNamespace() . $key); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function remove($key) { |
73
|
|
|
if (self::$cache->delete($this->getNamespace() . $key)) { |
74
|
|
|
return true; |
75
|
|
|
} else { |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function clear($prefix = '') { |
81
|
|
|
$prefix = $this->getNamespace() . $prefix . '*'; |
82
|
|
|
$it = null; |
83
|
|
|
self::$cache->setOption(\Redis::OPT_SCAN, \Redis::SCAN_RETRY); |
84
|
|
|
while ($keys = self::$cache->scan($it, $prefix)) { |
85
|
|
|
self::$cache->delete($keys); |
86
|
|
|
} |
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Set a value in the cache if it's not already stored |
92
|
|
|
* |
93
|
|
|
* @param string $key |
94
|
|
|
* @param mixed $value |
95
|
|
|
* @param int $ttl Time To Live in seconds. Defaults to 60*60*24 |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
|
public function add($key, $value, $ttl = 0) { |
99
|
|
|
// don't encode ints for inc/dec |
100
|
|
|
if (!is_int($value)) { |
101
|
|
|
$value = json_encode($value); |
102
|
|
|
} |
103
|
|
|
return self::$cache->setnx($this->getPrefix() . $key, $value); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Increase a stored number |
108
|
|
|
* |
109
|
|
|
* @param string $key |
110
|
|
|
* @param int $step |
111
|
|
|
* @return int | bool |
112
|
|
|
*/ |
113
|
|
|
public function inc($key, $step = 1) { |
114
|
|
|
return self::$cache->incrBy($this->getNamespace() . $key, $step); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Decrease a stored number |
119
|
|
|
* |
120
|
|
|
* @param string $key |
121
|
|
|
* @param int $step |
122
|
|
|
* @return int | bool |
123
|
|
|
*/ |
124
|
|
|
public function dec($key, $step = 1) { |
125
|
|
|
if (!$this->hasKey($key)) { |
126
|
|
|
return false; |
|
|
|
|
127
|
|
|
} |
128
|
|
|
return self::$cache->decrBy($this->getNamespace() . $key, $step); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Compare and set |
133
|
|
|
* |
134
|
|
|
* @param string $key |
135
|
|
|
* @param mixed $old |
136
|
|
|
* @param mixed $new |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
|
|
public function cas($key, $old, $new) { |
140
|
|
|
if (!is_int($new)) { |
141
|
|
|
$new = json_encode($new); |
142
|
|
|
} |
143
|
|
|
self::$cache->watch($this->getNamespace() . $key); |
144
|
|
View Code Duplication |
if ($this->get($key) === $old) { |
|
|
|
|
145
|
|
|
$result = self::$cache->multi() |
146
|
|
|
->set($this->getNamespace() . $key, $new) |
147
|
|
|
->exec(); |
148
|
|
|
return ($result === false) ? false : true; |
149
|
|
|
} |
150
|
|
|
self::$cache->unwatch(); |
151
|
|
|
return false; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Compare and delete |
156
|
|
|
* |
157
|
|
|
* @param string $key |
158
|
|
|
* @param mixed $old |
159
|
|
|
* @return bool |
160
|
|
|
*/ |
161
|
|
|
public function cad($key, $old) { |
162
|
|
|
self::$cache->watch($this->getNamespace() . $key); |
163
|
|
View Code Duplication |
if ($this->get($key) === $old) { |
|
|
|
|
164
|
|
|
$result = self::$cache->multi() |
165
|
|
|
->del($this->getNamespace() . $key) |
166
|
|
|
->exec(); |
167
|
|
|
return ($result === false) ? false : true; |
168
|
|
|
} |
169
|
|
|
self::$cache->unwatch(); |
170
|
|
|
return false; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function setTTL($key, $ttl) { |
174
|
|
|
self::$cache->expire($this->getNamespace() . $key, $ttl); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
static public function isAvailable() { |
178
|
|
|
return \OC::$server->getGetRedisFactory()->isAvailable(); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
|
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_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.