1 | <?php |
||
2 | |||
3 | namespace Mostafaznv\LaraCache; |
||
4 | |||
5 | use Illuminate\Support\Facades\Cache; |
||
0 ignored issues
–
show
|
|||
6 | |||
7 | class LaraCache |
||
8 | { |
||
9 | /** |
||
10 | * Update Cache Entity |
||
11 | * |
||
12 | * @param mixed $model |
||
13 | * @param string $name |
||
14 | * @param string $event |
||
15 | * @param CacheEntity|null $entity |
||
16 | * |
||
17 | * @return mixed |
||
18 | */ |
||
19 | public function update(mixed $model, string $name, string $event = '', CacheEntity $entity = null): mixed |
||
20 | { |
||
21 | return $model::cache()->update($name, $event, $entity); |
||
22 | } |
||
23 | |||
24 | /** |
||
25 | * Update All Cache Entities |
||
26 | * |
||
27 | * @param mixed $model |
||
28 | */ |
||
29 | public function updateAll(mixed $model = null): void |
||
30 | { |
||
31 | if ($model) { |
||
32 | $model::cache()->updateAll(); |
||
33 | } |
||
34 | else { |
||
35 | $list = self::list(); |
||
36 | |||
37 | /** @var mixed $model */ |
||
38 | foreach ($list as $model => $entities) { |
||
39 | $model::cache()->updateAll(); |
||
40 | } |
||
41 | } |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Delete Cache Entity |
||
46 | * |
||
47 | * @param mixed $model |
||
48 | * @param string $name |
||
49 | * @param bool $forever |
||
50 | * @return mixed |
||
51 | */ |
||
52 | public function delete(mixed $model, string $name, bool $forever = false): mixed |
||
53 | { |
||
54 | return $model::cache()->delete($name, $forever); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Delete All Cache Entities |
||
59 | * |
||
60 | * @param mixed $model |
||
61 | * @param bool $forever |
||
62 | */ |
||
63 | public function deleteAll(mixed $model = null, bool $forever = false): void |
||
64 | { |
||
65 | if ($model) { |
||
66 | $model::cache()->deleteAll($forever); |
||
67 | } |
||
68 | else { |
||
69 | $list = self::list(); |
||
70 | |||
71 | /** @var mixed $model */ |
||
72 | foreach ($list as $model => $entities) { |
||
73 | $model::cache()->deleteAll($forever); |
||
74 | } |
||
75 | } |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Retrieve Cache |
||
80 | * |
||
81 | * @param mixed $model |
||
82 | * @param string $name |
||
83 | * @param bool $withCacheData |
||
84 | * @return mixed |
||
85 | */ |
||
86 | public function retrieve(mixed $model, string $name, bool $withCacheData = false): mixed |
||
87 | { |
||
88 | return $model::cache()->get($name, $withCacheData); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Disable refresh cache on all events |
||
93 | * |
||
94 | * @param $model |
||
95 | */ |
||
96 | public function disable($model): void |
||
97 | { |
||
98 | $model::cache()->disable(); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Enable refresh cache on all events |
||
103 | * |
||
104 | * @param $model |
||
105 | */ |
||
106 | public function enable($model): void |
||
107 | { |
||
108 | $model::cache()->enable(); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Retrieve List of All Cache Entities |
||
113 | * |
||
114 | * @return array |
||
115 | */ |
||
116 | public function list(): array |
||
117 | { |
||
118 | $laracacheListKey = config('laracache.laracache-list'); |
||
119 | $driver = config('laracache.driver') ?? config('cache.default'); |
||
120 | |||
121 | return Cache::store($driver)->get($laracacheListKey, []); |
||
122 | } |
||
123 | } |
||
124 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: