3

使用PHP做 http pxory 缓存&代理, 使用redis做缓存,支持毫秒过期,拥有超高性能

 2 years ago
source link: https://blog.p2hp.com/archives/9308
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

使用PHP做 http pxory 缓存&代理, 使用redis做缓存,支持毫秒过期,拥有超高性能

使用PHP做 http pxory 缓存&代理, 使用 redis 做缓存,支持毫秒过期,拥有超高性能

<?php
//PHP http pxory 缓存&代理, 使用redis做缓存,支持毫秒过期,拥有超高性能
$url=@$_GET['url'];
if (empty($_GET['url'])) {
echo 'url is empty ';
exit;
$path=parse_url($url, PHP_URL_PATH);
$key=$path;
$redis = new Redis();
$redis->pconnect('127.0.0.1', 6379);
if ($content=$redis->get($key)) {
echo $content;
exit;
// Client
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOSIGNAL, 1); //注意,毫秒超时一定要设置这个
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 500); //超时毫秒,cURL 7.16.2中被加入。从PHP 5.2.3起可使用
$data = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
curl_close($ch);
if ($curl_errno > 0) {
echo "cURL Error ($curl_errno): $curl_error\n";
} else {
// $redis->setEx('key', 3600, 'value');//以秒为单位
$redis->pSetEx($key, 50000, $data); // 将参数中的字符串值设置为键的值,并带有生存时间 pSetEx以毫秒为单位.
echo $data;
exit;
<?php

 //PHP http pxory 缓存&代理, 使用redis做缓存,支持毫秒过期,拥有超高性能

 $url=@$_GET['url'];
if (empty($_GET['url'])) {
     echo 'url is empty ';
     exit;
}

 $path=parse_url($url, PHP_URL_PATH);
 $key=$path;

 $redis = new Redis();
 $redis->pconnect('127.0.0.1', 6379);

if ($content=$redis->get($key)) {
     echo $content;
     exit;
}

 // Client

 $ch = curl_init($url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_NOSIGNAL, 1);    //注意,毫秒超时一定要设置这个
 curl_setopt($ch, CURLOPT_TIMEOUT_MS, 500);  //超时毫秒,cURL 7.16.2中被加入。从PHP 5.2.3起可使用
 $data = curl_exec($ch);
 $curl_errno = curl_errno($ch);
 $curl_error = curl_error($ch);
 curl_close($ch);

if ($curl_errno > 0) {
     echo "cURL Error ($curl_errno): $curl_error\n";
} else {
   // $redis->setEx('key', 3600, 'value');//以秒为单位
     $redis->pSetEx($key, 50000, $data); //  将参数中的字符串值设置为键的值,并带有生存时间  pSetEx以毫秒为单位.
     echo $data;
     exit;
}

使用方法:

http://localhost/proxy.php?url=https://www.xxxxxxx.com:3004/rooms/5f392ac745f6cd207b5301db

经压力测试,性能超高.!!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK