Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Advertising

Mine
Sunday, March 11th, 2012 at 7:14:06am MDT 

  1. <?php
  2.  
  3. /**
  4.  * Класс обертка для работы с редисом, необходима для бенчмаркинга (нужно доделать),
  5.  * а также для учета ситуаций, когда соединение по каким-то причинам было оборвано
  6.  */
  7.  
  8. class RedisClient {
  9.   const REQUEST_STATE_OK = 1;
  10.   const REQUEST_STATE_FAIL = 2;
  11.  
  12.   const MAX_TRIES_COUNT = 3;
  13.   const PONG = "+PONG";
  14.  
  15.   protected $id;
  16.   protected $host;
  17.   protected $port;
  18.   protected $socket;
  19.   protected $db;
  20.   protected $handler;
  21.  
  22.   public function __construct($host, $port, $socket, $db, $serializer = Redis::SERIALIZER_IGBINARY) {
  23.     $this->host = $host;
  24.     $this->port = $port;
  25.     $this->socket = $socket;
  26.     $this->db = $db;
  27.  
  28.     $this->id = md5(microtime().mt_rand().(mt_rand() / mt_getrandmax()).microtime(true));
  29.  
  30.     $this->init();
  31.   }
  32.  
  33.   protected function init() {
  34.     $this->handler = new Redis();
  35.   }
  36.  
  37.   protected function checkConnection() {
  38.     if(is_object($this->handler)) {
  39.       return true;
  40.     }
  41.   }
  42.  
  43.   protected function cycledOp($op, array $args, $opLimit = self::MAX_TRIES_COUNT) {
  44.     $count = 0;
  45.     $ret = false;
  46.     $rstate = self::REQUEST_STATE_OK;
  47.     do {
  48.       try {
  49.         $ret = call_user_func_array($op, $args);
  50.         $rstate = self::REQUEST_STATE_OK;
  51.         break;
  52.       } catch(Exception $e) {
  53.         $ret = false;
  54.         ++$count;
  55.  
  56.         $rstate = self::REQUEST_STATE_FAIL;
  57.         Daemon::log('Error in redis op. Reason: '.$e->getMessage().'. Op: '.Debug::dump($op).'. Args: '.Debug::dump($args).' From: '.Debug::backtrace());
  58.  
  59.         $this->ping();
  60.       }
  61.     } while($count < $opLimit);
  62.     if($rstate == self::REQUEST_STATE_FAIL) {
  63.       Daemon::log('Epic FAIL: '.Debug::dump($op).Debug::backtrace());
  64.     }
  65.  
  66.     return $ret;
  67.   }
  68.  
  69.   public function __call($f, array $args) {
  70.     if(!method_exists($this->handler, $f)) {
  71.       Daemon::log('Try to call unknown method "'.$f.'" of Redis from: '.Debug::backtrace());
  72.       return false;
  73.     }
  74.  
  75.     return $this->cycledOp(array($this->handler, $f), $args);
  76.   }
  77.  
  78.   public function connect() {
  79.     $this->close();
  80.     $callback = array($this->handler, 'connect');
  81.     $args = array($this->host, $this->port, Config::REDIS_CONNECTION_TIMEOUT);
  82.  
  83.     $opCount = 0;
  84.     $maxOpCount = self::MAX_TRIES_COUNT;
  85.     do {
  86.       ++$opCount;
  87.       try {
  88.         $ret = $this->handler->connect($this->host, $this->port, Config::REDIS_CONNECTION_TIMEOUT);
  89.         if($ret) {
  90.           break;
  91.         }
  92.       } catch(Exception $e) {
  93.         Daemon::log('Error on RedisConnect('.$this->port.'). Reason: '.$e->getMessage());
  94.       }
  95.     } while($opCount < $maxOpCount);
  96.  
  97.     return;
  98.   }
  99.  
  100.   public function ping() {
  101.     try {
  102.       if($this->handler->ping() == self::PONG) {
  103.         return true;
  104.       }
  105.     } catch(Exception $e) {
  106.       //$this->close();
  107.       //$this->init();
  108.       Daemon::log('Error on ping! '.Debug::backtrace());
  109.       return $this->connect();
  110.     }
  111.   }
  112.  
  113.   public function close() {
  114.     try {
  115.       $this->handler->close();
  116.     } catch(Exception $e) {
  117.       // nothing do
  118.     }
  119.   }
  120.  
  121.   public function getObj() {
  122.     return $this->handler;
  123.   }
  124.  
  125.   public function getId() {
  126.     return $this->id;
  127.   }
  128.  
  129.   public function setHandler() {
  130.     return;
  131.   }
  132.  
  133.   public function setKeyExpire($key, $ttl) {
  134.     $this->expire($key, $ttl);
  135.   }
  136.  
  137.   public function regenerateKey() {
  138.     return;
  139.   }
  140.  
  141.   public function tearDown() {
  142.     $this->close();
  143.     $this->handler = null;
  144.   }
  145.  
  146.   public function isValid() {
  147.     return $this->port == 6300;
  148.   }
  149. }

advertising

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

fantasy-obligation
fantasy-obligation