<?php require 'vendor/autoload.php'; Predis\Autoloader::register(); $client = new Predis\Client(array('host' => '127.0.0.1', 'port' => 6379), array('prefix' => 'php:')); $client->set("string:my_key", "Hello World"); $client->get("string:my_key"); # "Hello World" $client->incr("string:counter"); $client->mget(array("string:my_key", "string:counter")); # array('Hello World', '2') $client->rpush("list:my_list", "item1", "item2"); $client->lpop("list:my_list"); # 'item1' $client->hset("set:redis_book", "title", "Redis Essentials"); $client->hgetall("set:redis_book"); # array('title' => 'Redis Essentials') $client->sadd("set:users", "alice", "bob"); $client->smembers("set:users"); # array('bob', 'alice') $client->zadd("sorted_set:programmers", 1940, "Alan Kay"); $client->zadd("sorted_set:programmers", 1912, "Alan Turing"); $client->zrange("sorted_set:programmers", 0, -1, "withscores"); # array('Alan Turing' => 1912, 'Alan Kay' => 1940)
/** * 保存记录至redis * @access private * @param string $value 保存值 * @return boolean 成功返回true,失败返回false */ private function save_item_in_redis($value = '') { require './include/Predis/Autoloader.php'; Predis\Autoloader::register(); try { $r = new Predis\Client(); $r->connect('127.0.0.1', 6379); $my_log_len = $r->llen($this->config->item('encryption_key')); if ($my_log_len < 21) { $r->rpush($this->config->item('encryption_key'), $value); } else { $r->lpop($this->config->item('encryption_key')); $r->rpush($this->config->item('encryption_key'), $value); } return TRUE; } catch (Exception $e) { echo $e->getMessage(); return FALSE; } }
/** * 完善daemon处理函数,此函数必备 * * */ function daemonFunc() { require dirname(__FILE__) . '/../../config/testUI/config.php'; $redis = new Predis\Client($_config['redis_server']); $http = new Http(); while ($this->subProcessCheck()) { //处理队列 $case_data = $redis->lpop($_config['queue_name']); if (empty($case_data)) { break; } else { $arr = json_decode($case_data, true); $url = $arr['host'] . $arr['url']; $query = $arr['param']; $method = strtoupper($arr['method']); //拼装表单提交数据 $formdata = array(); $temp_arry = explode('&', $query); foreach ($temp_arry as $item) { list($k, $v) = explode('=', $item); $formdata[$k] = $v; } //判断是否需要token if (isset($arr['token'])) { $formdata['token'] = $arr['token']; } if ($method == 'GET') { $http->get($url, $formdata); } else { $http->post($url, $formdata); } $res = $http->getContent(); //此处增加返回结果的判断 $result = $arr['result']; if ($result == $res) { $res_test = 1; } else { $res_test = 0; } // $req['url'] = $url; // $req['data'] = $formdata; //$result =array(); file_put_contents(realpath(dirname(__FILE__)) . '/../../output/testUI.log', $res_test . '|' . $result . '|' . $res . '|' . $url . '|' . json_encode($formdata) . "\n", FILE_APPEND); } //增加处理数,不增加处理数,就需要子进程本身有退出机制。 //$this->requestCount++; //释放时间片 usleep(5000); } }
// queue, respond with the help session data and additional data needed to connect. If there isn't // a customer available on the queue, respond with status code 204 NO CONTENT. // // Response: (JSON encoded) // * `apiKey`: The OpenTok API Key used to connect to the customer // * `sessionId`: The OpenTok Session ID used to the connect to the customer // * `token`: The OpenTok Token used to connect to the customer // * `customerName`: The customer's name // * `problemText`: The customer's problem description // // NOTE: This request allows anonymous access, but if user authentication is required then the // identity of the request should be verified (often times with session cookies) before a valid // response is given. $app->delete('/help/queue', function () use($app, $redis, $opentok, $config) { // Dequeue the next help session $redisResponse = $redis->lpop(HELP_QUEUE_KEY); if (!handleRedisError($redisResponse, $app, 'Não foi possível atender a uma sessão de vídeo chat da fila.')) { return; } $helpSessionKey = $redisResponse; if (empty($helpSessionKey)) { // The queue was empty $app->response->setStatus(204); } else { $redisResponse = $redis->hgetall($helpSessionKey); if (!handleRedisError($redisResponse, $app, 'Não foi possível ler a sessão de vídeo chat.')) { return; } $helpSessionData = $redisResponse; $responseData = array('apiKey' => $config->opentok('key'), 'sessionId' => $helpSessionData['sessionId'], 'token' => $opentok->generateToken($helpSessionData['sessionId']), 'customerName' => $helpSessionData['customerName'], 'problemText' => $helpSessionData['problemText']); // Once the help session is dequeued, we also clean it out of the storage.
<?php // Mock server to pool requests and responses // Test cases add stub responses then rake actual requests // Use redis to communicate with the test require_once __DIR__ . '/../../vendor/autoload.php'; $errorResponseJson = '{"error":{"message":"Mock response is not prepared","type":"api_error","caused_by":"service"}}'; $client = new Predis\Client(); $request = array('method' => $_SERVER['REQUEST_METHOD'], 'request_uri' => $_SERVER['REQUEST_URI'], 'body' => file_get_contents("php://input")); $client->rpush('mdl_webpay_test_requests', serialize($request)); $head = $client->lpop('mdl_webpay_test_responses'); if ($head === NULL) { http_response_code(500); header('Content-Type: application/json'); print $errorResponseJson; } else { $response = unserialize($head); http_response_code($response['status_code']); header('Content-Type: application/json'); print $response['body']; }