1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mostafaznv\LaraCache; |
4
|
|
|
|
5
|
|
|
use Mostafaznv\LaraCache\DTOs\CacheData; |
6
|
|
|
use Mostafaznv\LaraCache\DTOs\CacheEvent; |
7
|
|
|
use Mostafaznv\LaraCache\Jobs\RefreshCache; |
8
|
|
|
use Mostafaznv\LaraCache\Traits\InteractsWithCache; |
9
|
|
|
use Mostafaznv\LaraCache\Utils\RefreshDebouncer; |
10
|
|
|
|
11
|
|
|
class Cache |
12
|
|
|
{ |
13
|
|
|
use InteractsWithCache; |
|
|
|
|
14
|
|
|
|
15
|
|
|
public function get(string $name, bool $withCacheData = false): mixed |
16
|
|
|
{ |
17
|
|
|
$cache = $this->retrieve($name); |
18
|
|
|
|
19
|
|
|
if ($withCacheData) { |
20
|
|
|
return $cache; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
return $cache->value; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function update(string $name): CacheData |
27
|
|
|
{ |
28
|
|
|
$this->updateLaraCacheModelsList(); |
29
|
|
|
|
30
|
|
|
return $this->updateCacheEntity($name); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function updateAll(): void |
34
|
|
|
{ |
35
|
|
|
$this->updateLaraCacheModelsList(); |
36
|
|
|
|
37
|
|
|
foreach ($this->model::cacheEntities() as $entity) { |
38
|
|
|
$this->updateCacheEntity( |
39
|
|
|
name: $entity->name, |
40
|
|
|
entity: $entity |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function delete(string $name, bool $forever = false): CacheData |
46
|
|
|
{ |
47
|
|
|
return $this->deleteCacheEntity($name, $forever); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function deleteAll(bool $forever = false): void |
51
|
|
|
{ |
52
|
|
|
foreach ($this->model::cacheEntities() as $entity) { |
53
|
|
|
$this->deleteCacheEntity($entity->name, $forever, $entity); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function refresh(CacheEvent $event): void |
58
|
|
|
{ |
59
|
|
|
if ($this->model::$isEnabled) { |
60
|
|
|
$this->updateLaraCacheModelsList(); |
61
|
|
|
|
62
|
|
|
foreach ($this->model::cacheEntities() as $entity) { |
63
|
|
|
if ($entity->debounce) { |
64
|
|
|
$this->initCache($entity, $entity->getTtl()); |
65
|
|
|
|
66
|
|
|
RefreshDebouncer::dispatch( |
67
|
|
|
model: $this->model, |
68
|
|
|
name: $entity->name, |
69
|
|
|
queueConnection: $entity->queueConnection, |
70
|
|
|
queueName: $entity->queueName, |
71
|
|
|
wait: $entity->debounceWaitTime |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
else if ($entity->isQueueable) { |
75
|
|
|
$this->initCache($entity, $entity->getTtl()); |
76
|
|
|
|
77
|
|
|
RefreshCache::dispatch($this->model, $entity->name, $event) |
78
|
|
|
->onConnection($entity->queueConnection) |
79
|
|
|
->onQueue($entity->queueName); |
80
|
|
|
} |
81
|
|
|
else { |
82
|
|
|
$this->updateCacheEntity($entity->name, $event, $entity); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function disable(): void |
89
|
|
|
{ |
90
|
|
|
$this->model::$isEnabled = false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function enable(): void |
94
|
|
|
{ |
95
|
|
|
$this->model::$isEnabled = true; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|