| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace ByJG\Cache\Psr16; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Memcached; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Psr\Log\NullLogger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class MemcachedEngine extends BaseCacheEngine | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * @var Memcached | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     protected $memCached = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     protected $logger = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     protected $servers = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 21 |  | View Code Duplication |     public function __construct($servers = null, $logger = null) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $this->servers = (array)$servers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         if (is_null($servers)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             $this->servers = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |                 '127.0.0.1:11211' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $this->logger = $logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         if (is_null($logger)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             $this->logger = new NullLogger(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     protected function fixKey($key) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         return "cache-" . $key; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     protected function lazyLoadMemCachedServers() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         if (is_null($this->memCached)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             $this->memCached = new Memcached(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             foreach ($this->servers as $server) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 $data = explode(":", $server); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 $this->memCached->addServer($data[0], $data[1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 $stats = $this->memCached->getStats(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 if (!isset($stats[$server]) || $stats[$server]['pid'] === -1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                     throw new \Exception("Memcached server $server is down"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param string $key The object KEY | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param int $default IGNORED IN MEMCACHED. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @return mixed Description | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     public function get($key, $default = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $this->lazyLoadMemCachedServers(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $value = $this->memCached->get($this->fixKey($key)); | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 66 |  | View Code Duplication |         if ($this->memCached->getResultCode() !== Memcached::RES_SUCCESS) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             $this->logger->info("[Memcached] Cache '$key' missed with status " . $this->memCached->getResultCode()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             return $default; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         return unserialize($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param string $key The object Key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @param object $value The object to be cached | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param int $ttl The time to live in seconds of this objects | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @return bool If the object is successfully posted | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     public function set($key, $value, $ttl = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $this->lazyLoadMemCachedServers(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $this->memCached->set($this->fixKey($key), serialize($value), $ttl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $this->logger->info("[Memcached] Set '$key' result " . $this->memCached->getResultCode()); | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 86 |  | View Code Duplication |         if ($this->memCached->getResultCode() !== Memcached::RES_SUCCESS) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             $this->logger->error("[Memcached] Set '$key' failed with status " . $this->memCached->getResultCode()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         return $this->memCached->getResultCode() === Memcached::RES_SUCCESS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     public function delete($key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $this->lazyLoadMemCachedServers(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $this->memCached->delete($this->fixKey($key)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     public function isAvailable() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         if (!class_exists('\Memcached')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             $this->lazyLoadMemCachedServers(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         } catch (\Exception $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |     public function clear() | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |         $this->lazyLoadMemCachedServers(); | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |         $result = $this->memCached->flush(); | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |         return $result; | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     public function has($key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $this->lazyLoadMemCachedServers(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $this->memCached->get($this->fixKey($key)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         return ($this->memCached->getResultCode() === Memcached::RES_SUCCESS); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 133 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 134 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.