This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /** |
||
4 | * |
||
5 | * This file is part of the Apix Project. |
||
6 | * |
||
7 | * (c) Franck Cassedanne <franck at ouarz.net> |
||
8 | * |
||
9 | * @license http://opensource.org/licenses/BSD-3-Clause New BSD License |
||
10 | * |
||
11 | */ |
||
12 | |||
13 | namespace Apix\Cache; |
||
14 | |||
15 | /** |
||
16 | * Redis/PhpRedis cache wrapper. |
||
17 | * |
||
18 | * @author Franck Cassedanne <franck at ouarz.net> |
||
19 | */ |
||
20 | class Redis extends AbstractCache |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * Constructor. |
||
25 | * |
||
26 | * @param \Redis $redis A Redis client instance. |
||
27 | * @param array $options Array of options. |
||
28 | */ |
||
29 | 66 | public function __construct(\Redis $redis, array $options=null) |
|
30 | { |
||
31 | 66 | $options['atomicity'] = !isset($options['atomicity']) |
|
32 | 66 | || true === $options['atomicity'] |
|
33 | 66 | ? \Redis::MULTI |
|
34 | 66 | : \Redis::PIPELINE; |
|
35 | |||
36 | 66 | $this->options['serializer'] = 'php'; // null, php, igBinary, json, |
|
0 ignored issues
–
show
|
|||
37 | // msgpack |
||
38 | 66 | ||
39 | parent::__construct($redis, $options); |
||
40 | 66 | ||
41 | 66 | $this->setSerializer($this->options['serializer']); |
|
42 | 66 | $redis->setOption( \Redis::OPT_SERIALIZER, $this->getSerializer() ); |
|
43 | } |
||
44 | |||
45 | /** |
||
46 | * {@inheritdoc} |
||
47 | 33 | */ |
|
48 | public function loadKey($key) |
||
49 | 33 | { |
|
50 | $cache = $this->adapter->get($this->mapKey($key)); |
||
51 | 33 | ||
52 | return false === $cache ? null : $cache; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | 27 | */ |
|
58 | public function loadTag($tag) |
||
59 | 27 | { |
|
60 | $cache = $this->adapter->sMembers($this->mapTag($tag)); |
||
61 | 27 | ||
62 | return empty($cache) ? null : $cache; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | 45 | */ |
|
68 | public function save($data, $key, array $tags=null, $ttl=null) |
||
69 | 45 | { |
|
70 | $key = $this->mapKey($key); |
||
71 | 45 | ||
72 | 39 | if (null === $ttl || 0 === $ttl) { |
|
73 | 39 | $success = $this->adapter->set($key, $data); |
|
74 | 9 | } else { |
|
75 | $success = $this->adapter->setex($key, $ttl, $data); |
||
76 | } |
||
77 | 45 | ||
78 | 21 | if ($success && $this->options['tag_enable'] && !empty($tags)) { |
|
79 | 21 | $redis = $this->adapter->multi($this->options['atomicity']); |
|
80 | 21 | foreach ($tags as $tag) { |
|
81 | 21 | $redis->sAdd($this->mapTag($tag), $key); |
|
82 | 21 | } |
|
83 | 21 | $redis->exec(); |
|
84 | } |
||
85 | 45 | ||
86 | return $success; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | 3 | */ |
|
92 | public function clean(array $tags) |
||
93 | 3 | { |
|
94 | 3 | $items = array(); |
|
95 | 3 | foreach ($tags as $tag) { |
|
96 | 3 | $keys = $this->loadTag($tag); |
|
97 | 3 | if (is_array($keys)) { |
|
98 | 3 | array_walk_recursive( |
|
99 | $keys, |
||
100 | 3 | function ($key) use (&$items) { $items[] = $key; } |
|
101 | 3 | ); |
|
102 | 3 | } |
|
103 | 3 | $items[] = $this->mapTag($tag); |
|
104 | } |
||
105 | 3 | ||
106 | return (boolean) $this->adapter->del($items); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | 9 | */ |
|
112 | public function delete($key) |
||
113 | 9 | { |
|
114 | $key = $this->mapKey($key); |
||
115 | 9 | ||
116 | 9 | if ($this->options['tag_enable']) { |
|
117 | 9 | $tags = $this->adapter->keys($this->mapTag('*')); |
|
118 | 3 | if (!empty($tags)) { |
|
119 | 3 | $redis = $this->adapter->multi($this->options['atomicity']); |
|
120 | 3 | foreach ($tags as $tag) { |
|
121 | 3 | $redis->sRem($tag, $key); |
|
122 | 3 | } |
|
123 | 3 | $redis->exec(); |
|
124 | 9 | } |
|
125 | } |
||
126 | 9 | ||
127 | return (boolean) $this->adapter->del($key); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | 66 | */ |
|
133 | public function flush($all=false) |
||
134 | 66 | { |
|
135 | 3 | if (true === $all) { |
|
136 | return $this->adapter->flushAll(); |
||
137 | 66 | } |
|
138 | 66 | $items = array_merge( |
|
139 | 66 | $this->adapter->keys($this->mapTag('*')), |
|
140 | 66 | $this->adapter->keys($this->mapKey('*')) |
|
141 | ); |
||
142 | 66 | ||
143 | return (boolean) $this->adapter->del($items); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * {@inheritdoc} |
||
148 | * @param string $serializer |
||
149 | 66 | */ |
|
150 | public function setSerializer($serializer) |
||
151 | { |
||
152 | switch ($serializer) { |
||
153 | case 'json': |
||
154 | // $this->serializer = \Redis::SERIALIZER_JSON; |
||
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
37% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them. ![]() |
|||
155 | parent::setSerializer($serializer); |
||
156 | break; |
||
157 | |||
158 | case 'php': |
||
159 | 66 | $this->serializer = \Redis::SERIALIZER_PHP; |
|
160 | break; |
||
161 | 3 | ||
162 | 3 | // @codeCoverageIgnoreStart |
|
163 | case 'igBinary': |
||
164 | 66 | // igBinary is not always compiled on the host machine. |
|
165 | 66 | $this->serializer = \Redis::SERIALIZER_IGBINARY; |
|
166 | 66 | break; |
|
167 | |||
168 | 3 | case 'msgpack': |
|
169 | 3 | // available on PHP7 since msgpack 2.0.1 |
|
170 | 3 | $this->serializer = \Redis::SERIALIZER_MSGPACK; |
|
171 | 66 | break; |
|
172 | // @codeCoverageIgnoreEnd |
||
173 | |||
174 | default: |
||
175 | $this->serializer = \Redis::SERIALIZER_NONE; |
||
176 | 6 | } |
|
177 | } |
||
178 | 6 | ||
179 | /** |
||
180 | 6 | * {@inheritdoc} |
|
181 | 6 | */ |
|
182 | public function getTtl($key) |
||
183 | { |
||
184 | 6 | $ttl = $this->adapter->ttl($this->mapKey($key)); |
|
185 | |||
186 | if ($ttl == -2) { |
||
187 | return false; |
||
188 | 3 | } |
|
189 | |||
190 | return $ttl > -1 ? $ttl : 0; |
||
191 | } |
||
192 | |||
193 | } |
||
194 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.