| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace WebStream\Cache\Driver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use WebStream\DI\Injector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use WebStream\Container\Container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Redis | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @author Ryuichi Tanaka | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @since 2015/07/09 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @version 0.7 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class Redis implements ICache | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     use injector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @var Container キャッシュ依存コンテナ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private $cacheContainer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var string キャッシュ接頭辞 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $cachePrefix; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public function __construct(Container $cacheContainer) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->cacheContainer = $cacheContainer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->cachePrefix = $this->cacheContainer->cachePrefix . '.' . $this->cacheContainer->classPrefix . '.'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     public function add($key, $value, $ttl = 0, $overwrite = false): bool | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         if (!$this->isAvailableCacheLibrary()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |             return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         if (is_array($value)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |             $value = json_encode($value); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         $result = false; | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         if ($ttl > 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             if ($overwrite) { | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |                 $result = $this->cacheContainer->driver->setEx($key, $ttl, $value); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |                 $result = $this->cacheContainer->driver->set($key, $value, ['nx', 'ex' => $ttl]); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             if ($overwrite) { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |                 $result = $this->cacheContainer->driver->set($key, $value); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |                 $result = $this->cacheContainer->driver->setNx($key, $value); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         $this->logger->info("Execute cache save: " . $key); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     public function get($key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         if (!$this->isAvailableCacheLibrary()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $result = $this->cacheContainer->driver->get($key); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $this->logger->info("Execute cache read: " . $key); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         if ($result !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $this->logger->info("Execute cache read: " . $key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $this->logger->warn("Failed to read cache: " . $key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $result = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     public function delete($key): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         if (!$this->isAvailableCacheLibrary()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         if ($this->cacheContainer->driver->delete($key) > 0) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             $this->logger->info("Execute cache cleared: " . $key); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $this->logger->warn("Failed to clear cache: " . $key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     public function clear(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         if (!$this->isAvailableCacheLibrary()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $it = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $result = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $this->cacheContainer->driver->setOption($this->cacheContainer->redisOptPrefix, ""); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         while ($keys = $this->cacheContainer->driver->scan($it, "*")) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             $result *= $this->cacheContainer->driver->delete($keys); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $this->cacheContainer->driver->setOption($this->cacheContainer->redisOptPrefix, $this->cachePrefix); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         if ($result > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             $this->logger->info("Execute all cache cleared: " . $this->cachePrefix . "*"); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             $this->logger->warn("Failed to clear all cache: " . $this->cachePrefix . "*"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * キャッシュライブラリが使用可能か検査する | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * @return bool 使用可能でtrue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     private function isAvailableCacheLibrary(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         if ($this->cacheContainer->available) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         $this->logger->warn("Redis cache library is unavailable."); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 149 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 150 |  |  |  |