|
5 | 5 | namespace Rabbit\Cache;
|
6 | 6 |
|
7 | 7 | use Psr\SimpleCache\CacheInterface;
|
8 |
| -use Rabbit\Base\Table\Table; |
9 | 8 | use Rabbit\Parser\ParserInterface;
|
| 9 | +use Swoole\Table; |
10 | 10 |
|
11 |
| -/** |
12 |
| - * Class TableCache |
13 |
| - * @package Rabbit\Cache |
14 |
| - */ |
15 | 11 | class TableCache extends AbstractCache implements CacheInterface
|
16 | 12 | {
|
17 | 13 | private Table $tableInstance;
|
18 | 14 | private int $maxLive = 3000000;
|
19 |
| - private float $gcSleep = 0.01; |
| 15 | + private int $gcSleep = 100; |
20 | 16 | private int $gcProbability = 100;
|
21 | 17 |
|
22 |
| - /** |
23 |
| - * TableCache constructor. |
24 |
| - * @param int $size |
25 |
| - * @param int $dataLength |
26 |
| - * @param ParserInterface|null $serializer |
27 |
| - */ |
28 | 18 | public function __construct(int $size = 1024, private int $dataLength = 8192, private ?ParserInterface $serializer = null)
|
29 | 19 | {
|
30 | 20 | parent::__construct();
|
31 |
| - $this->tableInstance = $this->initCacheTable($size, $dataLength); |
32 |
| - } |
33 |
| - |
34 |
| - /** |
35 |
| - * @param int $size |
36 |
| - * @param int $dataLength |
37 |
| - * @return Table |
38 |
| - */ |
39 |
| - private function initCacheTable(int $size, int $dataLength): Table |
40 |
| - { |
41 |
| - $table = new Table('cache', $size); |
| 21 | + $table = new Table($size); |
42 | 22 | $table->column('expire', Table::TYPE_STRING, 11);
|
43 | 23 | $table->column('nextId', Table::TYPE_STRING, 35);
|
44 | 24 | $table->column('data', Table::TYPE_STRING, $dataLength);
|
45 | 25 | $table->create();
|
46 |
| - return $table; |
| 26 | + $this->tableInstance = $table; |
47 | 27 | }
|
48 | 28 |
|
49 | 29 | public function get($key, mixed $default = null): mixed
|
@@ -121,17 +101,20 @@ private function gc(bool $force = false): void
|
121 | 101 | {
|
122 | 102 | if ($force || mt_rand(0, 1000000) < $this->gcProbability) {
|
123 | 103 | $i = 100000;
|
124 |
| - $table = $this->tableInstance->table; |
125 |
| - foreach ($table as $key => $column) { |
| 104 | + $dels = []; |
| 105 | + foreach ($this->tableInstance as $key => $column) { |
126 | 106 | if ($column['expire'] > 0 && $column['expire'] < time()) {
|
127 |
| - $this->deleteValue($key); |
| 107 | + $dels[] = $key; |
128 | 108 | }
|
129 | 109 | $i--;
|
130 | 110 | if ($i <= 0) {
|
131 |
| - \Swoole\Coroutine::sleep($this->gcSleep); |
| 111 | + usleep($this->gcSleep); |
132 | 112 | $i = 100000;
|
133 | 113 | }
|
134 | 114 | }
|
| 115 | + foreach ($dels as $key) { |
| 116 | + $this->deleteValue($key); |
| 117 | + } |
135 | 118 | }
|
136 | 119 | }
|
137 | 120 |
|
@@ -175,7 +158,7 @@ public function delete($key): bool
|
175 | 158 | public function clear(): bool
|
176 | 159 | {
|
177 | 160 | $table = [];
|
178 |
| - foreach ($this->tableInstance->table as $key => $column) { |
| 161 | + foreach ($this->tableInstance as $key => $column) { |
179 | 162 | $table[] = $key;
|
180 | 163 | }
|
181 | 164 | $ret = true;
|
|
0 commit comments