|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Cache package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) Daniel González |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* @author Daniel González <[email protected]> |
|
12
|
|
|
* @author Arnold Daniels <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
declare(strict_types=1); |
|
16
|
|
|
|
|
17
|
|
|
namespace Desarrolla2\Cache; |
|
18
|
|
|
|
|
19
|
|
|
use Desarrolla2\Cache\Exception\CacheException; |
|
20
|
|
|
use Desarrolla2\Cache\Packer\PackerInterface; |
|
21
|
|
|
use Desarrolla2\Cache\Packer\NopPacker; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Apcu |
|
25
|
|
|
*/ |
|
26
|
|
|
class Apcu extends AbstractCache |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Create the default packer for this cache implementation |
|
30
|
|
|
* |
|
31
|
|
|
* @return PackerInterface |
|
32
|
|
|
*/ |
|
33
|
60 |
|
protected static function createDefaultPacker(): PackerInterface |
|
34
|
|
|
{ |
|
35
|
60 |
|
return new NopPacker(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
99 |
|
public function set($key, $value, $ttl = null) |
|
43
|
|
|
{ |
|
44
|
99 |
|
$ttlSeconds = $this->ttlToSeconds($ttl); |
|
45
|
|
|
|
|
46
|
79 |
|
if (isset($ttlSeconds) && $ttlSeconds <= 0) { |
|
47
|
2 |
|
return $this->delete($key); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
78 |
|
return apcu_store($this->keyToId($key), $this->pack($value), $ttlSeconds ?? 0); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
80 |
|
public function get($key, $default = null) |
|
57
|
|
|
{ |
|
58
|
80 |
|
$packed = apcu_fetch($this->keyToId($key), $success); |
|
59
|
|
|
|
|
60
|
62 |
|
return $success ? $this->unpack($packed) : $default; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritdoc} |
|
65
|
|
|
*/ |
|
66
|
22 |
|
public function has($key) |
|
67
|
|
|
{ |
|
68
|
22 |
|
return apcu_exists($this->keyToId($key)); |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritdoc} |
|
73
|
|
|
*/ |
|
74
|
42 |
|
public function delete($key) |
|
75
|
|
|
{ |
|
76
|
42 |
|
$id = $this->keyToId($key); |
|
77
|
|
|
|
|
78
|
24 |
|
return apcu_delete($id) || !apcu_exists($id); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
198 |
|
public function clear() |
|
85
|
|
|
{ |
|
86
|
198 |
|
return apcu_clear_cache(); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|