Skip to content

Commit 50a1e8b

Browse files
authored
Merge pull request #4635 from paulbalandan/cache-handler-optimize
[Cache] Allow covariant returns and optimize code
2 parents 042184b + 1635b47 commit 50a1e8b

File tree

6 files changed

+46
-94
lines changed

6 files changed

+46
-94
lines changed

system/Cache/Handlers/DummyHandler.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ class DummyHandler extends BaseHandler
2323
*/
2424
public function initialize()
2525
{
26-
// Nothing to see here...
2726
}
2827

2928
//--------------------------------------------------------------------
@@ -33,7 +32,7 @@ public function initialize()
3332
*
3433
* @param string $key Cache item name
3534
*
36-
* @return mixed
35+
* @return null
3736
*/
3837
public function get(string $key)
3938
{
@@ -49,7 +48,7 @@ public function get(string $key)
4948
* @param integer $ttl Time to live
5049
* @param Closure $callback Callback return value
5150
*
52-
* @return mixed
51+
* @return null
5352
*/
5453
public function remember(string $key, int $ttl, Closure $callback)
5554
{
@@ -108,7 +107,7 @@ public function deleteMatching(string $pattern)
108107
* @param string $key Cache ID
109108
* @param integer $offset Step/value to increase by
110109
*
111-
* @return mixed
110+
* @return boolean
112111
*/
113112
public function increment(string $key, int $offset = 1)
114113
{
@@ -123,7 +122,7 @@ public function increment(string $key, int $offset = 1)
123122
* @param string $key Cache ID
124123
* @param integer $offset Step/value to increase by
125124
*
126-
* @return mixed
125+
* @return boolean
127126
*/
128127
public function decrement(string $key, int $offset = 1)
129128
{
@@ -150,7 +149,7 @@ public function clean()
150149
* The information returned and the structure of the data
151150
* varies depending on the handler.
152151
*
153-
* @return mixed
152+
* @return null
154153
*/
155154
public function getCacheInfo()
156155
{
@@ -164,7 +163,7 @@ public function getCacheInfo()
164163
*
165164
* @param string $key Cache item name.
166165
*
167-
* @return mixed
166+
* @return null
168167
*/
169168
public function getMetaData(string $key)
170169
{
@@ -182,6 +181,4 @@ public function isSupported(): bool
182181
{
183182
return true;
184183
}
185-
186-
//--------------------------------------------------------------------
187184
}

system/Cache/Handlers/FileHandler.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function __construct(Cache $config)
7171
}
7272

7373
$this->mode = $config->file['mode'] ?? 0640;
74-
$this->prefix = (string) $config->prefix;
74+
$this->prefix = $config->prefix;
7575
}
7676

7777
//--------------------------------------------------------------------
@@ -81,7 +81,6 @@ public function __construct(Cache $config)
8181
*/
8282
public function initialize()
8383
{
84-
// Not to see here...
8584
}
8685

8786
//--------------------------------------------------------------------
@@ -190,7 +189,7 @@ public function deleteMatching(string $pattern)
190189
* @param string $key Cache ID
191190
* @param integer $offset Step/value to increase by
192191
*
193-
* @return mixed
192+
* @return boolean
194193
*/
195194
public function increment(string $key, int $offset = 1)
196195
{
@@ -223,7 +222,7 @@ public function increment(string $key, int $offset = 1)
223222
* @param string $key Cache ID
224223
* @param integer $offset Step/value to increase by
225224
*
226-
* @return mixed
225+
* @return bool
227226
*/
228227
public function decrement(string $key, int $offset = 1)
229228
{
@@ -268,7 +267,7 @@ public function clean()
268267
* The information returned and the structure of the data
269268
* varies depending on the handler.
270269
*
271-
* @return mixed
270+
* @return array|false
272271
*/
273272
public function getCacheInfo()
274273
{
@@ -534,6 +533,8 @@ protected function getFileInfo(string $file, $returnedValues = ['name', 'server_
534533
$returnedValues = explode(',', $returnedValues);
535534
}
536535

536+
$fileInfo = [];
537+
537538
foreach ($returnedValues as $key)
538539
{
539540
switch ($key)
@@ -565,8 +566,6 @@ protected function getFileInfo(string $file, $returnedValues = ['name', 'server_
565566
}
566567
}
567568

568-
return $fileInfo; // @phpstan-ignore-line
569+
return $fileInfo;
569570
}
570-
571-
//--------------------------------------------------------------------
572571
}

system/Cache/Handlers/MemcachedHandler.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class MemcachedHandler extends BaseHandler
5757
*/
5858
public function __construct(Cache $config)
5959
{
60-
$this->prefix = (string) $config->prefix;
60+
$this->prefix = $config->prefix;
6161

6262
if (! empty($config))
6363
{
@@ -269,7 +269,7 @@ public function deleteMatching(string $pattern)
269269
* @param string $key Cache ID
270270
* @param integer $offset Step/value to increase by
271271
*
272-
* @return mixed
272+
* @return integer|false
273273
*/
274274
public function increment(string $key, int $offset = 1)
275275
{
@@ -292,7 +292,7 @@ public function increment(string $key, int $offset = 1)
292292
* @param string $key Cache ID
293293
* @param integer $offset Step/value to increase by
294294
*
295-
* @return mixed
295+
* @return integer|false
296296
*/
297297
public function decrement(string $key, int $offset = 1)
298298
{
@@ -328,7 +328,7 @@ public function clean()
328328
* The information returned and the structure of the data
329329
* varies depending on the handler.
330330
*
331-
* @return mixed
331+
* @return array|false
332332
*/
333333
public function getCacheInfo()
334334
{
@@ -380,6 +380,6 @@ public function getMetaData(string $key)
380380
*/
381381
public function isSupported(): bool
382382
{
383-
return (extension_loaded('memcached') || extension_loaded('memcache'));
383+
return extension_loaded('memcached') || extension_loaded('memcache');
384384
}
385385
}

system/Cache/Handlers/PredisHandler.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class PredisHandler extends BaseHandler
5858
*/
5959
public function __construct(Cache $config)
6060
{
61-
$this->prefix = (string) $config->prefix;
61+
$this->prefix = $config->prefix;
6262

6363
if (isset($config->redis))
6464
{
@@ -101,10 +101,9 @@ public function initialize()
101101
*/
102102
public function get(string $key)
103103
{
104-
$data = array_combine([
105-
'__ci_type',
106-
'__ci_value',
107-
], $this->redis->hmget($key, ['__ci_type', '__ci_value'])
104+
$data = array_combine(
105+
['__ci_type', '__ci_value'],
106+
$this->redis->hmget($key, ['__ci_type', '__ci_value'])
108107
);
109108

110109
if (! isset($data['__ci_type'], $data['__ci_value']) || $data['__ci_value'] === false)
@@ -180,7 +179,7 @@ public function save(string $key, $value, int $ttl = 60)
180179
*/
181180
public function delete(string $key)
182181
{
183-
return ($this->redis->del($key) === 1);
182+
return $this->redis->del($key) === 1;
184183
}
185184

186185
//--------------------------------------------------------------------
@@ -194,7 +193,6 @@ public function delete(string $key)
194193
*/
195194
public function deleteMatching(string $pattern)
196195
{
197-
$deleted = 0;
198196
$matchedKeys = [];
199197

200198
foreach (new Keyspace($this->redis, $pattern) as $key)
@@ -213,7 +211,7 @@ public function deleteMatching(string $pattern)
213211
* @param string $key Cache ID
214212
* @param integer $offset Step/value to increase by
215213
*
216-
* @return mixed
214+
* @return integer
217215
*/
218216
public function increment(string $key, int $offset = 1)
219217
{
@@ -228,7 +226,7 @@ public function increment(string $key, int $offset = 1)
228226
* @param string $key Cache ID
229227
* @param integer $offset Step/value to increase by
230228
*
231-
* @return mixed
229+
* @return integer
232230
*/
233231
public function decrement(string $key, int $offset = 1)
234232
{
@@ -255,7 +253,7 @@ public function clean()
255253
* The information returned and the structure of the data
256254
* varies depending on the handler.
257255
*
258-
* @return mixed
256+
* @return array
259257
*/
260258
public function getCacheInfo()
261259
{

system/Cache/Handlers/RedisHandler.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class RedisHandler extends BaseHandler
5757
*/
5858
public function __construct(Cache $config)
5959
{
60-
$this->prefix = (string) $config->prefix;
60+
$this->prefix = $config->prefix;
6161

6262
if (! empty($config))
6363
{
@@ -72,7 +72,7 @@ public function __construct(Cache $config)
7272
*/
7373
public function __destruct()
7474
{
75-
if ($this->redis) // @phpstan-ignore-line
75+
if (isset($this->redis))
7676
{
7777
$this->redis->close();
7878
}
@@ -218,7 +218,7 @@ public function delete(string $key)
218218
{
219219
$key = $this->prefix . $key;
220220

221-
return ($this->redis->del($key) === 1);
221+
return $this->redis->del($key) === 1;
222222
}
223223

224224
//--------------------------------------------------------------------
@@ -262,7 +262,7 @@ public function deleteMatching(string $pattern)
262262
* @param string $key Cache ID
263263
* @param integer $offset Step/value to increase by
264264
*
265-
* @return mixed
265+
* @return integer
266266
*/
267267
public function increment(string $key, int $offset = 1)
268268
{
@@ -279,7 +279,7 @@ public function increment(string $key, int $offset = 1)
279279
* @param string $key Cache ID
280280
* @param integer $offset Step/value to increase by
281281
*
282-
* @return mixed
282+
* @return integer
283283
*/
284284
public function decrement(string $key, int $offset = 1)
285285
{
@@ -308,7 +308,7 @@ public function clean()
308308
* The information returned and the structure of the data
309309
* varies depending on the handler.
310310
*
311-
* @return mixed
311+
* @return array
312312
*/
313313
public function getCacheInfo()
314314
{
@@ -358,6 +358,4 @@ public function isSupported(): bool
358358
{
359359
return extension_loaded('redis');
360360
}
361-
362-
//--------------------------------------------------------------------
363361
}

0 commit comments

Comments
 (0)