| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Dtc\QueueBundle\Redis; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | class PhpRedis implements RedisInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |     protected $redis; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |     protected $maxRetries; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     public function __construct(\Redis $redis, $maxRetries = 5) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |         $this->redis = $redis; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |         $this->maxRetries = $maxRetries; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     public function zAdd($zkey, $score, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         return $this->redis->zadd($zkey, $score, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     public function set($key, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         return $this->redis->set($key, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public function get($key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         return $this->redis->get($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function setEx($key, $seconds, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         return $this->redis->setex($key, $seconds, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     public function lRem($lKey, $count, $value) | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         return $this->redis->lrem($lKey, $count, $value); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function lPush($lKey, array $values) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $args = $values; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         array_unshift($args, $lKey); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         return call_user_func_array([$this->redis, 'lPush'], $args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function lRange($lKey, $start, $stop) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         return $this->redis->lrange($lKey, $start, $stop); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     public function del(array $keys) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return $this->redis->del($keys); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public function zRem($zkey, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         return $this->redis->zrem($zkey, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     public function zPop($key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $retries = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         do { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             $this->redis->watch($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $elements = $this->redis->zrange($key, 0, 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             if (empty($elements)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 $this->redis->unwatch(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $result = $this->redis->multi() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 ->zrem($key, $elements[0]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 ->exec(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             if (false !== $result) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 return $elements[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             ++$retries; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } while ($retries < $this->maxRetries); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     public function zPopByMaxScore($key, $max) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $retries = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         do { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             $this->redis->watch($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             $elements = $this->redis->zrangebyscore($key, 0, $max, ['limit' => [0, 1]]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             if (empty($elements)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 $this->redis->unwatch(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             $result = $this->redis->multi() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 ->zrem($key, $elements[0]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 ->exec(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             if (false !== $result) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 return $elements[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             ++$retries; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         } while ($retries < $this->maxRetries); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 109 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 110 |  |  |  |