| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Silviooosilva\CacheerPhp\Service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Closure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Silviooosilva\CacheerPhp\Cacheer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Silviooosilva\CacheerPhp\Helpers\CacheerHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Silviooosilva\CacheerPhp\Utils\CacheDataFormatter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class CacheRetriever | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     private Cacheer $cacheer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     public function __construct(Cacheer $cacheer) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         $this->cacheer = $cacheer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     public function getCache(string $cacheKey, string $namespace = '', int|string $ttl = 3600) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $cacheData = $this->cacheer->cacheStore->getCache($cacheKey, $namespace, $ttl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $this->cacheer->syncState(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         if ($this->cacheer->isSuccess() && ($this->cacheer->isCompressionEnabled() || $this->cacheer->getEncryptionKey() !== null)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             $cacheData = CacheerHelper::recoverFromStorage($cacheData, $this->cacheer->isCompressionEnabled(), $this->cacheer->getEncryptionKey()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         return $this->cacheer->isFormatted() ? new CacheDataFormatter($cacheData) : $cacheData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function getMany(array $cacheKeys, string $namespace = '', int|string $ttl = 3600) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $cachedData = $this->cacheer->cacheStore->getMany($cacheKeys, $namespace, $ttl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->cacheer->syncState(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         if ($this->cacheer->isSuccess() && ($this->cacheer->isCompressionEnabled() || $this->cacheer->getEncryptionKey() !== null)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             foreach ($cachedData as &$data) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 $data = CacheerHelper::recoverFromStorage($data, $this->cacheer->isCompressionEnabled(), $this->cacheer->getEncryptionKey()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         return $this->cacheer->isFormatted() ? new CacheDataFormatter($cachedData) : $cachedData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     public function getAll(string $namespace = '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $cachedData = $this->cacheer->cacheStore->getAll($namespace); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $this->cacheer->syncState(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         if ($this->cacheer->isSuccess() && ($this->cacheer->isCompressionEnabled() || $this->cacheer->getEncryptionKey() !== null)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             foreach ($cachedData as &$data) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 $data = CacheerHelper::recoverFromStorage($data, $this->cacheer->isCompressionEnabled(), $this->cacheer->getEncryptionKey()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return $this->cacheer->isFormatted() ? new CacheDataFormatter($cachedData) : $cachedData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public function getAndForget(string $cacheKey, string $namespace = '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $cachedData = $this->getCache($cacheKey, $namespace); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         if (!empty($cachedData)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $this->cacheer->setInternalState("Cache retrieved and deleted successfully!", true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $this->cacheer->clearCache($cacheKey, $namespace); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             return $cachedData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     public function remember(string $cacheKey, int|string $ttl, Closure $callback) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $cachedData = $this->getCache($cacheKey, ttl: $ttl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         if (!empty($cachedData)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             return $cachedData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $cacheData = $callback(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $this->cacheer->putCache($cacheKey, $cacheData, ttl: $ttl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         return $cacheData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     public function rememberForever(string $cacheKey, Closure $callback) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         return $this->remember($cacheKey, 31536000 * 1000, $callback); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |     public function has(string $cacheKey, string $namespace = ''): void | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         $this->cacheer->cacheStore->has($cacheKey, $namespace); | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |         $this->cacheer->syncState(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 95 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |  |