protected function Write($desc, $data) { try { $obj = new stdClass(); $obj->group = $this->group; $obj->desc = $desc; $obj->time = microtime(); $obj->data = $data; $time = time(); $md5 = md5($this->group); $guid = $this->GetGuid(); /* @var $p Predis_CommandPipeline */ $p = $this->predis->pipeline(); $p->hset($this->redis_prefix . '.groups.name', $md5, $this->group); $p->hset($this->redis_prefix . '.groups.last', $md5, $time); if (!$this->requestStarted) { $p->lpush($this->redis_prefix . '.group:' . $md5 . '.log', $guid); $details = array(); $details['start'] = microtime(true); if (isset($_SERVER['REQUEST_URI'])) { $details['url'] = $_SERVER['REQUEST_URI']; $details['method'] = $_SERVER['REQUEST_METHOD']; $details['ip'] = $_SERVER['REMOTE_HOST']; } $p->set($this->redis_prefix . '.group:' . $md5 . '.request:' . $guid . '.details', json_encode($details)); $this->requestStarted = true; } $p->lpush($this->redis_prefix . '.group:' . $md5 . '.request:' . $guid . '.logs', json_encode($data)); $p->execute(); } catch (Exception $ex) { $this->HandleException($ex); } }
/** * @see sfCache */ public function set($key, $data, $lifetime = null) { $lifetime = $this->getLifetime($lifetime); if ($lifetime < 1) { $response = $this->remove($key); } else { $pkey = $this->getKey($key); $mkey = $this->getKey($key, '_lastmodified'); $pipe = $this->redis->pipeline(); $pipe->mset(array($pkey => $data, $mkey => $_SERVER['REQUEST_TIME'])); $pipe->expire($pkey, $lifetime); $pipe->expire($mkey, $lifetime); $response = $pipe->execute(); } return $response; }
<?php require_once 'SharedConfigurations.php'; // When you have a whole set of consecutive commands to send to // a redis server, you can use a pipeline to improve performances. $redis = new Predis_Client($single_server); $pipe = $redis->pipeline(); $pipe->ping(); $pipe->flushdb(); $pipe->incrby('counter', 10); $pipe->incrby('counter', 30); $pipe->exists('counter'); $pipe->get('counter'); $pipe->mget('does_not_exist', 'counter'); $replies = $pipe->execute(); print_r($replies); /* OUTPUT: Array ( [0] => 1 [1] => 1 [2] => 10 [3] => 40 [4] => 1 [5] => 40 [6] => Array ( [0] => [1] => 40 )