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\Packer\PackerInterface; |
20
|
|
|
use Desarrolla2\Cache\Packer\SerializePacker; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Memory |
24
|
|
|
*/ |
25
|
|
|
class Memory extends AbstractCache |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Limit the amount of entries |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
protected $limit = PHP_INT_MAX; |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $cache = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $cacheTtl = []; |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Create the default packer for this cache implementation. |
47
|
|
|
* {@internal NopPacker might fail PSR-16, as cached objects would change} |
48
|
|
|
* |
49
|
|
|
* @return PackerInterface |
50
|
|
|
*/ |
51
|
163 |
|
protected static function createDefaultPacker(): PackerInterface |
52
|
|
|
{ |
53
|
163 |
|
return new SerializePacker(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Make a clone of this object. |
58
|
|
|
* Set by cache reference, thus using the same pool. |
59
|
|
|
* |
60
|
|
|
* @return static |
61
|
|
|
*/ |
62
|
6 |
|
protected function cloneSelf(): AbstractCache |
63
|
|
|
{ |
64
|
6 |
|
$clone = clone $this; |
65
|
|
|
|
66
|
6 |
|
$clone->cache =& $this->cache; |
67
|
6 |
|
$clone->cacheTtl =& $this->cacheTtl; |
68
|
|
|
|
69
|
6 |
|
return $clone; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Set the max number of items |
74
|
|
|
* |
75
|
|
|
* @param int $limit |
76
|
|
|
*/ |
77
|
1 |
|
protected function setLimitOption($limit) |
78
|
|
|
{ |
79
|
1 |
|
$this->limit = (int)$limit ?: PHP_INT_MAX; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the max number of items |
84
|
|
|
* |
85
|
|
|
* @return int |
86
|
|
|
*/ |
87
|
|
|
protected function getLimitOption() |
88
|
|
|
{ |
89
|
|
|
return $this->limit; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
142 |
|
public function get($key, $default = null) |
97
|
|
|
{ |
98
|
142 |
|
if (!$this->has($key)) { |
99
|
42 |
|
return $default; |
100
|
|
|
} |
101
|
|
|
|
102
|
74 |
|
$id = $this->keyToId($key); |
103
|
|
|
|
104
|
74 |
|
return $this->unpack($this->cache[$id]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
181 |
|
public function has($key) |
111
|
|
|
{ |
112
|
181 |
|
$id = $this->keyToId($key); |
113
|
|
|
|
114
|
109 |
|
if (!isset($this->cacheTtl[$id])) { |
115
|
41 |
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
81 |
|
if ($this->cacheTtl[$id] <= time()) { |
119
|
8 |
|
unset($this->cache[$id], $this->cacheTtl[$id]); |
120
|
8 |
|
return false; |
121
|
|
|
} |
122
|
|
|
|
123
|
77 |
|
return true; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
199 |
|
public function set($key, $value, $ttl = null) |
130
|
|
|
{ |
131
|
199 |
|
if (count($this->cache) >= $this->limit) { |
132
|
1 |
|
$deleteKey = key($this->cache); |
133
|
1 |
|
unset($this->cache[$deleteKey], $this->cacheTtl[$deleteKey]); |
134
|
|
|
} |
135
|
|
|
|
136
|
199 |
|
$id = $this->keyToId($key); |
137
|
|
|
|
138
|
163 |
|
$this->cache[$id] = $this->pack($value); |
139
|
163 |
|
$this->cacheTtl[$id] = $this->ttlToTimestamp($ttl) ?? PHP_INT_MAX; |
140
|
|
|
|
141
|
123 |
|
return true; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
*/ |
147
|
80 |
|
public function delete($key) |
148
|
|
|
{ |
149
|
80 |
|
$id = $this->keyToId($key); |
150
|
44 |
|
unset($this->cache[$id], $this->cacheTtl[$id]); |
151
|
|
|
|
152
|
44 |
|
return true; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
410 |
|
public function clear() |
159
|
|
|
{ |
160
|
410 |
|
$this->cache = []; |
161
|
410 |
|
$this->cacheTtl = []; |
162
|
|
|
|
163
|
410 |
|
return true; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|