Php Memcache 错误案例和处理

Php Memcache Error Case and handling

我将每个客户评论的前 50 条评论存储在 memcache.I 中,想知道我会遇到哪种错误以及如何处理该错误。 我正在使用此代码:

$memcache = new Memcache();
$memcache->connect('127.0.0.1', 11212);
//Key to get user total from cache
$user_total     = md5("user_total_".$user);        
//Get User total from cache
$get_user_total = $memcache->get($user_total);
$total_reviews  = 0;

if($get_user_total){
    $total_reviews = !empty($get_user_total) ? $get_user_total : 0;
}else{

    $sql = " SELECT  COUNT(*) as total FROM user AS u ";
    $sql.= " JOIN onj_ship_awb AS sa ON(u.user_id = sa.user_id) ";
    $sql.= " JOIN onj_customer_feedback AS cf  ON (cf.ship_awb_id = sa.id) ";
    $sql.= " WHERE u.username = '' AND u.password = ''  AND cf.status = '1' AND cf.insert_time  <= ( CURDATE() - INTERVAL 3 DAY ) ";
    $result = $con->query( $sql );
    $total_reviews  = $result->fetch_assoc();
    $total_reviews  = $total_reviews['total'];
    $memcache->set($user_total,$total_reviews,MEMCACHE_COMPRESSED,TTL);
    $total_reviews = !empty($total_reviews) ? $total_reviews : 0;
}  

例如,如果我连接的端口不是 11211.It 的抛出错误。

Memcache::connect(): Server 127.0.0.1 (tcp 11212, udp 0) failed with: Connection refused (111)

那么我该如何处理这种错误以及我应该处理哪些其他情况?

这是我们检查 Memcache 连接状态的方法。

$memcache = new Memcache();
$memcache_status = @$memcache->connect('127.0.0.1', 11212);
if($memcache_status){
    //Success
}else{
    //Fail
}