示例#1
0
文件: Redis.php 项目: JerryCR/php-2
 /**
  * Performs the test.
  *
  * @return \Jyxo\Beholder\Result
  */
 public function run()
 {
     // The redis extension or Predis library is required
     if (!extension_loaded('redis') && !class_exists('\\Predis\\Client')) {
         return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::NOT_APPLICABLE, 'Extension redis or Predis library required');
     }
     $random = md5(uniqid(time(), true));
     $key = 'beholder-' . $random;
     $value = $random;
     // Status label
     $description = (false !== filter_var($this->host, FILTER_VALIDATE_IP) ? gethostbyaddr($this->host) : $this->host) . ':' . $this->port . '?database=' . $this->database;
     // Connection
     if (extension_loaded('redis')) {
         $redis = new \Redis();
         if (false === $redis->connect($this->host, $this->port, 2)) {
             return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Connection error %s', $description));
         }
     } else {
         $redis = new \Predis\Client(array('host' => $this->host, 'port' => $this->port));
         if (false === $redis->connect()) {
             return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Connection error %s', $description));
         }
     }
     // Select database
     if (false === $redis->select($this->database)) {
         return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Database error %s', $description));
     }
     // Saving
     if (false === $redis->set($key, $value)) {
         if ($redis instanceof \Redis) {
             $redis->close();
         } else {
             $redis->quit();
         }
         return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Write error %s', $description));
     }
     // Check
     $check = $redis->get($key);
     if (false === $check || $check !== $value) {
         if ($redis instanceof \Redis) {
             $redis->close();
         } else {
             $redis->quit();
         }
         return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Read error %s', $description));
     }
     // Deleting
     if (false === $redis->del($key)) {
         if ($redis instanceof \Redis) {
             $redis->close();
         } else {
             $redis->quit();
         }
         return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Delete error %s', $description));
     }
     // Disconnect
     if ($redis instanceof \Redis) {
         $redis->close();
     } else {
         $redis->quit();
     }
     // OK
     return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::SUCCESS, $description);
 }
#!/usr/bin/php
<?php 
require 'vendor/autoload.php';
$client = new Predis\Client(['host' => '127.0.0.1', 'port' => 6379]);
$userCount = array('pmcount' => rand(5, 20), 'secount' => rand(5, 10), 'ssecount' => rand(10, 30), 'tlcount' => rand(20, 40));
$channelName = 'redis_data';
$message = 'This is a test message from php redis publisher.';
//Publishing message to redis server
$client->publish($channelName, $message);
$client->quit();
echo "published test message to channel [{$channelName}] ";