redis多查询连接管理

0sgqnhkj  于 2021-06-09  发布在  Redis
关注(0)|答案(0)|浏览(238)

我有一个 php 脚本,其中我必须执行多个偶尔查询。所以,我想知道什么是连接和执行redis查询的最佳方式。
单向,连接一次,保持连接活动,从脚本开始到结束执行所有查询,然后关闭连接

//Request comes and some work is done on the request like validation etc
//then
$redis = new Redis()->pconnect();   
$redis->hMSet();

//Some other codes are run like sending email or similar <-Long Codes
//then
$redis->hSet();

//If everything was good send updates to other users <-Long Codes
//then
$redis->setBit();

//Some More Work <-Long Codes
//then
$redis->hGet();
$redis->close();

双向,为每个redis操作创建连接,并为每个操作关闭和重复

//Request comes and some work is done on the request like validation etc
//then
$redis = new Redis()->pconnect();   
$redis->hMSet();
$redis->close();

//Some other codes are run like sending email or similar <-Long Codes
//then
$redis = new Redis()->pconnect();   
$redis->hSet();
$redis->close();

//If everything was good send updates to other users <-Long Codes
//then
$redis = new Redis()->pconnect();   
$redis->setBit();
$redis->close();

//Some More Work <-Long Codes
//then
$redis = new Redis()->pconnect();   
$redis->hGet();
$redis->close();

第一个似乎更好,但我在想,如果它可能是一个坏的做法,保持连接活着,而运行其他代码。这一切都是因为redis能够高效地处理每秒数百万个请求,而我的任何错误都不会导致某些结果。我读到创建一个连接是资源密集型的,但我想确定更好的方法。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题