/**
  *
  */
 public function main()
 {
     $key = self::getClassKey();
     $fileHandle = fopen($this->logPath->getSafePath('.', "Redis.log"), "a");
     $loops = 0;
     $writesPerSecond = 500;
     $maxRunTime = 30;
     $sleepTime = intval(1000000 / $writesPerSecond);
     $endTime = time() + $maxRunTime;
     $count = 0;
     while (time() < $endTime) {
         //A nil multi-bulk when no element could be popped and the timeout expired.
         //A two-element multi-bulk with the first element being the name of the key where an element was popped and the second element being the value of the popped element.
         //Timeout should be less than socket timeout.
         $logEntries = $this->redisClient->blpop($key, 5);
         if ($logEntries) {
             for ($x = 0; $x < count($logEntries); $x += 2) {
                 $logEntry = $logEntries[$x + 1];
                 $written = fwrite($fileHandle, $logEntry);
                 if ($written) {
                     printf("wrote log entry %d.\n", $count);
                     $count++;
                 } else {
                     //Todo - push back all remaining log entries.
                     //$this->redisClient->lpush($key, $logEntry);
                     throw new \RuntimeException("Failed to write to log file,");
                 }
             }
         }
         usleep($sleepTime);
         $loops++;
     }
     fclose($fileHandle);
     echo "Fin.\n";
 }
 public static function getRedis()
 {
     require_once CODE_BASE2 . '/util/redis/RedisClient.class.php';
     try {
         if ($RedisClient == null) {
             $RedisClient = new RedisClient(RedisConfig::$GROUP_WEBAPP);
         }
     } catch (Exception $e) {
     }
     return $RedisClient->getMasterRedis('wapmsc');
 }
 /**
  * 获取redis client实例
  * @param string $key
  * @return IRedis
  */
 static function open($key = 'default')
 {
     $instance =& self::$INSTANCES[$key];
     if (isset($instance) == false) {
         $config = ConfigFactory::get('redis', $key);
         if (empty($config)) {
             die("Undefined Redis Config \"{$key}\"");
         }
         $instance = new RedisClient();
         $instance->setServers($config['servers']);
     }
     return $instance;
 }
 public function __invoke(RequestInterface $requestInterface, ResponseInterface $responseInterface, $next)
 {
     if (!$this->clientIdentifier) {
         if (!empty($_SERVER['HTTP_CF_CONNECTING_IP'])) {
             $reformattedSource = preg_replace("/[.:]/", "_", $_SERVER['HTTP_CF_CONNECTING_IP']);
         } else {
             $reformattedSource = preg_replace("/[.:]/", "_", $_SERVER['REMOTE_ADDR']);
         }
     } else {
         $reformattedSource = $this->clientIdentifier;
     }
     $requestCount = count($this->redis->keys(sprintf("rl.%s.*", $reformattedSource)));
     if ($requestCount > $this->maxRequests) {
         $responseInterface = $responseInterface->withStatus(429);
         $responseInterface->getBody()->write("Rate limit exceeded.");
         return $responseInterface;
     } else {
         $key = sprintf("rl.%s.%s", $reformattedSource, microtime(true));
         $this->redis->set($key, 1);
         $this->redis->expire($key, $this->expire);
         $responseInterface = $next($requestInterface, $responseInterface);
     }
     return $responseInterface;
 }
 /**
  * Constructor.
  * @return void
  */
 public function init()
 {
     $req = $this;
     $job = $this->job = new ComplexJob(function () use($req) {
         // called when job is done
         $req->wakeup();
         // wake up the request immediately
     });
     $redis = RedisClient::getInstance();
     $redis->lpush('mylist', microtime(true));
     // just pushing something
     $job('testquery', function ($name, $job) use($redis) {
         // registering job named 'testquery'
         $redis->lrange('mylist', 0, 10, function ($redis) use($name, $job) {
             // calling lrange Redis command
             $job->setResult($name, $redis->result);
             // setting job result
         });
     });
     $job();
     // let the fun begin
     $this->sleep(1, true);
     // setting timeout
 }
 function onClose(\swoole_client $cli)
 {
     if ($this->wait_send) {
         $this->redis->freeConnection($cli->sock, $this);
         call_user_func($this->callback, "timeout", false);
     }
 }
 public static function getPaths($id, $fromCache = false)
 {
     if ($fromCache) {
         $redis = RedisClient::getInstance(RedisConfig::$SERVER_COMMON);
         if (!is_array($id)) {
             $key = RedisKeys::ROOT_PATH_SET_ . $id;
             $set = $redis->sMembers($key);
             return empty($set) ? array() : $set;
         } else {
             $retHash = array();
             foreach ($id as $mId) {
                 $key = RedisKeys::ROOT_PATH_SET_ . $mId;
                 $set = $redis->sMembers($key);
                 $retHash[$mId] = empty($set) ? array() : $set;
             }
             return $retHash;
         }
     } else {
         RootRelationInterface::syncToRedis(array('manager_id' => $id));
         return self::getPaths($id, true);
     }
 }
 public static function syncAllToRedis()
 {
     $managerList = RootManagerInterface::getList(array('field' => 'id'));
     if (empty($managerList)) {
         return;
     }
     $managerIds = array_column($managerList, 'id');
     $retList = self::getList();
     $hash = array();
     foreach ($retList as $row) {
         $hash[$row['manager_id']][] = $row['path'];
     }
     // 对每个权限组进行排序
     foreach ($hash as $mId => $val) {
         sort($hash[$mId], SORT_STRING);
     }
     $redis = RedisClient::getInstance(RedisConfig::$SERVER_COMMON);
     $keys = $redis->keys(RedisKeys::ROOT_PATH_SET_ . '*');
     $redis->delete($keys);
     // 保存到redis
     foreach ($managerIds as $mId) {
         $pathSet = Arr::get($mId, $hash, array());
         $key = RedisKeys::ROOT_PATH_SET_ . $mId;
         foreach ($pathSet as $val) {
             $redis->sAdd($key, $val);
         }
     }
 }
 public function unwatch($key)
 {
     $this->redis->send("UNWATCH \"{$key}\"");
 }
 /**
  * 合并声望每周排行数据
  */
 protected function merge_week_fame()
 {
     $source_redis = new RedisClient(array('host' => $this->_source_host, 'port' => $this->_redis_port));
     $target_redis = new RedisClient(array('host' => $this->_target_host, 'port' => $this->_redis_port));
     $keys = $source_redis->keys("data|player_week_fame_sorted_set|*");
     if ($keys) {
         foreach ($keys as $key) {
             $data = $source_redis->zsets_all($key);
             if ($data) {
                 foreach ($data as $player_id) {
                     $score = $source_redis->zsets_score($key, $player_id);
                     $result = $target_redis->zsets_add($key, $player_id, $score);
                     if (!$result) {
                         file_put_contents("merge_week_fame.log", "{$key} {$player_id}\n", FILE_APPEND);
                     }
                 }
             }
         }
     }
     $source_redis->close();
     $target_redis->close();
 }
 function __construct()
 {
     $this->redis_client = RedisClient::getPredisObject();
 }
 function getTypeObj($key)
 {
     $r = RedisClient::getPredisObject();
     $type = $r->type($key);
     return $this->findType($type);
 }
Exemple #13
0
 /**
  * @brief Insert time need to monitor into redis 
  * @param $value Value of time. e.g.The response time when call an interface 
  * @return false or string
  */
 public function insert($value)
 {
     if (empty($this->_itemName) || !is_numeric($value) || $this->_ratio <= 0) {
         return 'PARAM_ERROR';
     }
     try {
         $redis = RedisClient::getInstance('finance');
         $time = date('Y-m-d H:i', time() + 60);
         $key = $time . '#' . $this->_itemName;
         if ($redis->rpush($key, $value)) {
             $redis->expire($key, 2 * 60);
         }
     } catch (Exception $e) {
         return 'REDIS_ERROR';
     }
     return false;
 }