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 | |||
10 | class Cache |
||
11 | { |
||
12 | use InteractsWithCache; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
13 | |||
14 | public function get(string $name, bool $withCacheData = false): mixed |
||
15 | { |
||
16 | $cache = $this->retrieve($name); |
||
17 | |||
18 | if ($withCacheData) { |
||
19 | return $cache; |
||
20 | } |
||
21 | |||
22 | return $cache->value; |
||
23 | } |
||
24 | |||
25 | public function update(string $name): CacheData |
||
26 | { |
||
27 | $this->updateLaraCacheModelsList(); |
||
28 | |||
29 | return $this->updateCacheEntity($name); |
||
30 | } |
||
31 | |||
32 | public function updateAll(): void |
||
33 | { |
||
34 | $this->updateLaraCacheModelsList(); |
||
35 | |||
36 | foreach ($this->model::cacheEntities() as $entity) { |
||
37 | $this->updateCacheEntity( |
||
38 | name: $entity->name, |
||
39 | entity: $entity |
||
40 | ); |
||
41 | } |
||
42 | } |
||
43 | |||
44 | public function delete(string $name, bool $forever = false): CacheData |
||
45 | { |
||
46 | return $this->deleteCacheEntity($name, $forever); |
||
47 | } |
||
48 | |||
49 | public function deleteAll(bool $forever = false): void |
||
50 | { |
||
51 | foreach ($this->model::cacheEntities() as $entity) { |
||
52 | $this->deleteCacheEntity($entity->name, $forever, $entity); |
||
53 | } |
||
54 | } |
||
55 | |||
56 | public function refresh(CacheEvent $event): void |
||
57 | { |
||
58 | if ($this->model::$isEnabled) { |
||
59 | $this->updateLaraCacheModelsList(); |
||
60 | |||
61 | foreach ($this->model::cacheEntities() as $entity) { |
||
62 | if ($entity->isQueueable) { |
||
63 | $this->initCache($entity, $entity->getTtl()); |
||
64 | |||
65 | RefreshCache::dispatch($this->model, $entity->name, $event) |
||
66 | ->onConnection($entity->queueConnection) |
||
67 | ->onQueue($entity->queueName); |
||
68 | } |
||
69 | else { |
||
70 | $this->updateCacheEntity($entity->name, $event, $entity); |
||
71 | } |
||
72 | } |
||
73 | } |
||
74 | } |
||
75 | |||
76 | public function disable(): void |
||
77 | { |
||
78 | $this->model::$isEnabled = false; |
||
79 | } |
||
80 | |||
81 | public function enable(): void |
||
82 | { |
||
83 | $this->model::$isEnabled = true; |
||
84 | } |
||
85 | } |
||
86 |