|
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; |
|
|
|
|
|
|
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
|
|
|
return $this->updateCacheEntity($name); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function updateAll(): void |
|
31
|
|
|
{ |
|
32
|
|
|
foreach ($this->model::cacheEntities() as $entity) { |
|
33
|
|
|
$this->updateCacheEntity( |
|
34
|
|
|
name: $entity->name, |
|
35
|
|
|
entity: $entity |
|
36
|
|
|
); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function delete(string $name, bool $forever = false): CacheData |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->deleteCacheEntity($name, $forever); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function deleteAll(bool $forever = false): void |
|
46
|
|
|
{ |
|
47
|
|
|
foreach ($this->model::cacheEntities() as $entity) { |
|
48
|
|
|
$this->deleteCacheEntity($entity->name, $forever, $entity); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function refresh(CacheEvent $event): void |
|
53
|
|
|
{ |
|
54
|
|
|
if ($this->model::$isEnabled) { |
|
55
|
|
|
foreach ($this->model::cacheEntities() as $entity) { |
|
56
|
|
|
if ($entity->isQueueable) { |
|
57
|
|
|
RefreshCache::dispatch($this->model, $entity->name, $event); |
|
58
|
|
|
} |
|
59
|
|
|
else { |
|
60
|
|
|
$this->updateCacheEntity($entity->name, $event, $entity); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function disable(): void |
|
67
|
|
|
{ |
|
68
|
|
|
$this->model::$isEnabled = false; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function enable(): void |
|
72
|
|
|
{ |
|
73
|
|
|
$this->model::$isEnabled = true; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|