| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Mostafaznv\LaraCache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Support\Facades\Cache as CacheFacade; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Database\Eloquent\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Mostafaznv\LaraCache\CacheEntity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Mostafaznv\LaraCache\Jobs\RefreshCache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Mostafaznv\LaraCache\Utils\Helpers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class Cache | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     public static string $created  = 'created'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     public static string $updated  = 'updated'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     public static string $deleted  = 'deleted'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     public static string $restored = 'restored'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private mixed $model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     public function __construct(string $model) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $this->model = $model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private function driver(): ?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         return config('laracache.driver') ?? config('cache.default'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private function findCacheEntity(string $name, CacheEntity $entity = null): ?CacheEntity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         if (is_null($entity)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             foreach ($this->model::cacheEntities() as $cacheEntity) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                 if ($cacheEntity->name === $name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                     return $cacheEntity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         return $entity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     private function entityIsCallable(CacheEntity $entity, string $event = ''): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         return $event == '' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             or ($event == self::$created and $entity->refreshAfterCreate) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             or ($event == self::$updated and $entity->refreshAfterUpdate) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             or ($event == self::$deleted and $entity->refreshAfterDelete) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             or ($event == self::$restored and $entity->refreshAfterRestore); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     private function isQueueable(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return config('laracache.queue') ?? false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     private function updateCacheEntity(string $name, string $event = '', CacheEntity $entity = null): mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $entity = $this->findCacheEntity($name, $entity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         if ($entity) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $driver = $this->driver(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             if ($this->entityIsCallable($entity, $event)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 $value = call_user_func($entity->cacheClosure); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 if ($entity->forever) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                     CacheFacade::store($driver)->forever($entity->name, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                     if ($entity->validForRestOfDay) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                         $ttl = Helpers::timeToEndOfDay(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                     else if ($entity->validForRestOfWeek) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                         $ttl = Helpers::timeToEndOfWeek(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                     else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                         $ttl = $entity->ttl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                     CacheFacade::store($driver)->put($entity->name, $value, $ttl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 return CacheFacade::store($driver)->get($entity->name, $entity->default); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             throw new Exception("Cache entity [$name] not found. please check if [$name] exists in " . $this->model); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     private function retrieve(string $name): mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $driver = $this->driver(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         foreach ($this->model::cacheEntities() as $entity) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             if ($entity->name == $name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 $value = CacheFacade::store($driver)->get($entity->name, $entity->default); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 if ($value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                     return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                     return $this->updateCacheEntity($name, '', $entity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         throw new Exception("Cache entity [$name] not found. please check if [$name] exists in " . $this->model); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     public function refresh(Model $model, string $event): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         if ($this->model::$isEnabled) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             if ($this->isQueueable()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                 RefreshCache::dispatch($model, $event); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                 foreach ($this->model::cacheEntities() as $entity) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                     $this->updateCacheEntity($entity->name, $event, $entity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     public function get(string $name): mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         return $this->retrieve($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     public function update(string $name): mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         return $this->updateCacheEntity($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     public function updateAll(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         foreach ($this->model::cacheEntities() as $entity) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             $this->updateCacheEntity( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |                 name: $entity->name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |                 entity: $entity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |     public function disable(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |         $this->model::$isEnabled = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     public function enable(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         $this->model::$isEnabled = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 161 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 162 |  |  |  |