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; }
/** * 合并声望每周排行数据 */ 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(); }