phpredis connect && pconnect分析
phpredis connect && pconnect分析
落叶纷飞v 发表于1年前
phpredis connect && pconnect分析
  • 发表于 1年前
  • 阅读 51
  • 收藏 1
  • 点赞 0
  • 评论 0

【腾讯云】新注册用户域名抢购1元起>>>   

摘要: connect和pconnect连接方式的一些区别

phpredis拓展提供了两种连接redis的方式:connect和pconnect

现在利用strace工具来观察两者的区别:

1.connect

代码如下:

<?php

//echo "hello world, php";
// test redis connect

$redis = new Redis();
$redis->connect("127.0.0.1", 2180);

$retval = $redis->set("zhang", '123');

var_dump($retval);

假设php-fpm子进程的pid = 3000,通过strace工具跟踪进程的运行情况:

// 重定向到/tmp/testStrace.log中
ubuntu-root# strace -p 3000 -s 1024 -0 /tmp/testStrace.log

通过浏览器访问test.php文件,然后查看strace捕获到的信息如下:

accept(0, {sa_family=AF_INET, sin_port=htons(45960), sin_addr=inet_addr("127.0.0.1")}, [16]) = 4
// 参数返回进程的各种事件,函数返回值就是返回实际运行的时间
times({tms_utime=0, tms_stime=2, tms_cutime=0, tms_cstime=0}) = 1718119770
// 文件描述符4
poll([{fd=4, events=POLLIN}], 1, 5000)  = 1 ([{fd=4, revents=POLLIN}])
...
// 读取http请求的信息
read(4, "\f\0QUERY_STRING\16\3REQUEST_METHODGET\f\0CONTENT_TYPE\16\0CONTENT_LENGTH\v\tSCRIPT_NAME/test.php\v\tREQUEST_URI/test.php\f\tDOCUMENT_URI/test.php\r\20DOCUMENT_ROOT/opt/htdocs2/php\17\10SERVER_PROTOCOLHTTP/1.1\21\7GATEWAY_INTERFACECGI/1.1\17\vSERVER_SOFTWAREnginx/1.8.0\v\rREMOTE_ADDR192.168.40.19\v\4REMOTE_PORT8221\v\rSERVER_ADDR192.168.40.33\v\2SERVER_PORT80\v\fSERVER_NAMEwww.test.com\17\3REDIRECT_STATUS200\17\31SCRIPT_FILENAME/www/htdocs2/php/test.php\v\tSCRIPT_NAME/test.php\t\fHTTP_HOSTwww.test.com\17\nHTTP_CONNECTIONkeep-alive\22\tHTTP_CACHE_CONTROLmax-age=0\36\1HTTP_UPGRADE_INSECURE_REQUESTS1\17lHTTP_USER_AGENTMozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36\vJHTTP_ACCEPTtext/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\24\23HTTP_ACCEPT_ENCODINGgzip, deflate, sdch\24\16HTTP_ACCEPT_LANGUAGEzh-CN,zh;q=0.8\0\0\0\0\0\0", 840) = 840
...
...
...
// 打开需要执行的php文件
open("/mnt/hgfs/htdocs2/php/test.php", O_RDONLY) = 9
...
mmap(NULL, 177, PROT_READ, MAP_SHARED, 9, 0) = 0x7f6a0d7ee000
...
munmap(0x7f6a0d7ee000, 177)             = 0
// 关闭打开的文件
close(9)                                = 0

// 创建socket链接 socket id = 9
socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 9
...
connect(9, {sa_family=AF_INET, sin_port=htons(9000), sin_addr=inet_addr("192.168.40.19")}, 16) = -1 EINPROGRESS (Operation now in progress)
select(10, [9], [9], [9], {0, 200000})  = 0 (Timeout)
...
...
// 创建socket链接 scoket id = 10
socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 10
...
...
connect(10, {sa_family=AF_INET, sin_port=htons(2180), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)
poll([{fd=10, events=POLLIN|POLLOUT|POLLERR|POLLHUP}], 1, 60000) = 1 ([{fd=10, revents=POLLOUT}])
...
...
...
poll([{fd=10, events=POLLIN|POLLPRI|POLLERR|POLLHUP}], 1, 0) = 0 (Timeout)
sendto(10, "*3\r\n$3\r\nSET\r\n$5\r\nzhang\r\n$3\r\n123\r\n", 33, MSG_DONTWAIT, NULL, 0) = 33
poll([{fd=10, events=POLLIN|POLLPRI|POLLERR|POLLHUP}], 1, 0) = 1 ([{fd=10, revents=POLLIN}])
recvfrom(10, "+", 1, MSG_PEEK, NULL, NULL) = 1
poll([{fd=10, events=POLLIN|POLLERR|POLLHUP}], 1, 60000) = 1 ([{fd=10, revents=POLLIN}])
recvfrom(10, "+OK\r\n", 8192, MSG_DONTWAIT, NULL, NULL) = 5
...
...
// 关闭socket链接 socket id = 10
close(10)                               = 0
...
write(4, "\1\6\0\1\0\251\7\0X-Powered-By: PHP/5.6.8\r\nContent-type: text/html; charset=UTF-8\r\n\r\n<pre class='xdebug-var-dump' dir='ltr'><small>boolean</small> <font color='#75507b'>true</font>\n</pre>\0\0\0\0\0\0\0\1\3\0\1\0\10\0\0\0\0\0\0\0\0\0\0", 200) = 200
shutdown(4, SHUT_WR)                    = 0
recvfrom(4, "\1\5\0\1\0\0\0\0", 8, 0, NULL, NULL) = 8
recvfrom(4, "", 8, 0, NULL, NULL)       = 0
close(4)                                = 0
...

 

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 0
博文 11
码字总数 3712
×
落叶纷飞v
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: