Repository navigation
Expand file tree
/
Copy pathCoroutineMemoryDriver.php
More file actions
123 lines (101 loc) · 2.79 KB
/
Copy pathCoroutineMemoryDriver.php
File metadata and controls
123 lines (101 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Cache\Driver;
use Hyperf\Cache\Collector\CoroutineMemory;
use Hyperf\Cache\Collector\CoroutineMemoryKey;
class CoroutineMemoryDriver extends Driver implements KeyCollectorInterface
{
public function get($key, $default = null): mixed
{
return $this->getCollection()->get($key, $default);
}
public function set($key, $value, $ttl = null): bool
{
$this->getCollection()->offsetSet($key, $value);
return true;
}
public function delete($key): bool
{
$this->getCollection()->offsetUnset($key);
return true;
}
public function clear(): bool
{
$this->getCollection()->clear();
return true;
}
public function getMultiple($keys, $default = null): iterable
{
$result = [];
foreach ($keys as $key) {
$result[$key] = $this->get($key, $default);
}
return $result;
}
public function setMultiple($values, $ttl = null): bool
{
foreach ($values as $key => $value) {
$this->set($key, $value, $ttl);
}
return true;
}
public function deleteMultiple($keys): bool
{
foreach ($keys as $key) {
$this->delete($key);
}
return true;
}
public function has($key): bool
{
return $this->getCollection()->has($key);
}
public function fetch(string $key, $default = null): array
{
if (! $this->has($key)) {
return [false, $default];
}
return [true, $this->get($key)];
}
public function clearPrefix(string $prefix): bool
{
return $this->getCollection()->clearPrefix($prefix);
}
public function addKey(string $collector, string $key): bool
{
$instance = CoroutineMemoryKey::instance();
$data = $instance->get($collector, []);
$data[] = $key;
$instance->put($collector, $data);
return true;
}
public function keys(string $collector): array
{
return CoroutineMemoryKey::instance()->get($collector, []);
}
public function delKey(string $collector, string ...$key): bool
{
$instance = CoroutineMemoryKey::instance();
$result = [];
$data = $instance->get($collector, []);
foreach ($data as $item) {
if (! in_array($item, $key)) {
$result[] = $item;
}
}
$instance->put($collector, $result);
return true;
}
protected function getCollection()
{
return CoroutineMemory::instance();
}
}