@@ -7,75 +7,75 @@ |
||
7 | 7 | |
8 | 8 | class MemoryAdapter implements StoreInterface |
9 | 9 | { |
10 | - protected $cleanExpired = 30; |
|
11 | - protected $lastClean = 0; |
|
12 | - |
|
13 | - protected $store = []; |
|
14 | - protected $ttlKeys = []; |
|
15 | - |
|
16 | - public function get(string $key): int |
|
17 | - { |
|
18 | - $this->isNeedCleanExpired() && $this->cleanExpired(); |
|
19 | - return $this->store[$key] ?? 0; |
|
20 | - } |
|
21 | - |
|
22 | - public function __construct(int $cleanExpired = 30) |
|
23 | - { |
|
24 | - $this->cleanExpired = $cleanExpired; |
|
25 | - $this->lastClean = time(); |
|
26 | - } |
|
27 | - |
|
28 | - public function inc(string $key, int $ttl = 60): int |
|
29 | - { |
|
30 | - $value = $this->get($key); |
|
31 | - $this->store[$key] = ++$value; |
|
32 | - |
|
33 | - if ($value === 1) { |
|
34 | - $this->ttlKeys[$key] = time() + $ttl; |
|
35 | - } |
|
36 | - |
|
37 | - return $value; |
|
38 | - } |
|
39 | - |
|
40 | - public function reset(string $key): bool |
|
41 | - { |
|
42 | - unset($this->store[$key], $this->ttlKeys[$key]); |
|
43 | - return true; |
|
44 | - } |
|
45 | - |
|
46 | - public function isExceeded(string $key, int $max): bool |
|
47 | - { |
|
48 | - $value = $this->get($key); |
|
49 | - return $value >= $max; |
|
50 | - } |
|
51 | - |
|
52 | - protected function isNeedCleanExpired(): bool |
|
53 | - { |
|
54 | - return ($this->lastClean + $this->cleanExpired) <= time(); |
|
55 | - } |
|
56 | - |
|
57 | - protected function cleanExpired(): void |
|
58 | - { |
|
59 | - $current = time(); |
|
60 | - |
|
61 | - $this->ttlKeys = array_filter($this->ttlKeys, function(int $time, string $key) use ($current) { |
|
62 | - $isExpired = $time < $current; |
|
63 | - if ($isExpired) { |
|
64 | - unset($this->store[$key]); |
|
65 | - } |
|
66 | - |
|
67 | - return !$isExpired; |
|
68 | - }, ARRAY_FILTER_USE_BOTH); |
|
69 | - } |
|
70 | - |
|
71 | - public function ttl(string $key): ?int |
|
72 | - { |
|
73 | - if (array_key_exists($key, $this->ttlKeys)) { |
|
74 | - $ttl = $this->ttlKeys[$key] - time(); |
|
75 | - return $ttl > 0 ? $ttl : 0; |
|
76 | - } |
|
77 | - |
|
78 | - return null; |
|
79 | - } |
|
10 | + protected $cleanExpired = 30; |
|
11 | + protected $lastClean = 0; |
|
12 | + |
|
13 | + protected $store = []; |
|
14 | + protected $ttlKeys = []; |
|
15 | + |
|
16 | + public function get(string $key): int |
|
17 | + { |
|
18 | + $this->isNeedCleanExpired() && $this->cleanExpired(); |
|
19 | + return $this->store[$key] ?? 0; |
|
20 | + } |
|
21 | + |
|
22 | + public function __construct(int $cleanExpired = 30) |
|
23 | + { |
|
24 | + $this->cleanExpired = $cleanExpired; |
|
25 | + $this->lastClean = time(); |
|
26 | + } |
|
27 | + |
|
28 | + public function inc(string $key, int $ttl = 60): int |
|
29 | + { |
|
30 | + $value = $this->get($key); |
|
31 | + $this->store[$key] = ++$value; |
|
32 | + |
|
33 | + if ($value === 1) { |
|
34 | + $this->ttlKeys[$key] = time() + $ttl; |
|
35 | + } |
|
36 | + |
|
37 | + return $value; |
|
38 | + } |
|
39 | + |
|
40 | + public function reset(string $key): bool |
|
41 | + { |
|
42 | + unset($this->store[$key], $this->ttlKeys[$key]); |
|
43 | + return true; |
|
44 | + } |
|
45 | + |
|
46 | + public function isExceeded(string $key, int $max): bool |
|
47 | + { |
|
48 | + $value = $this->get($key); |
|
49 | + return $value >= $max; |
|
50 | + } |
|
51 | + |
|
52 | + protected function isNeedCleanExpired(): bool |
|
53 | + { |
|
54 | + return ($this->lastClean + $this->cleanExpired) <= time(); |
|
55 | + } |
|
56 | + |
|
57 | + protected function cleanExpired(): void |
|
58 | + { |
|
59 | + $current = time(); |
|
60 | + |
|
61 | + $this->ttlKeys = array_filter($this->ttlKeys, function(int $time, string $key) use ($current) { |
|
62 | + $isExpired = $time < $current; |
|
63 | + if ($isExpired) { |
|
64 | + unset($this->store[$key]); |
|
65 | + } |
|
66 | + |
|
67 | + return !$isExpired; |
|
68 | + }, ARRAY_FILTER_USE_BOTH); |
|
69 | + } |
|
70 | + |
|
71 | + public function ttl(string $key): ?int |
|
72 | + { |
|
73 | + if (array_key_exists($key, $this->ttlKeys)) { |
|
74 | + $ttl = $this->ttlKeys[$key] - time(); |
|
75 | + return $ttl > 0 ? $ttl : 0; |
|
76 | + } |
|
77 | + |
|
78 | + return null; |
|
79 | + } |
|
80 | 80 | |
81 | 81 | } |
82 | 82 | \ No newline at end of file |