duane9322 2011-10-10 22:32
浏览 95

Memcached高插入/更新导致死锁

I am recording unique page views using memcached and storing them in db at 15 mins interval. Whenever number of users grow memcached gives me following error:

Memcache::get(): Server localhost (tcp 10106) failed with: Failed reading line from stream (0)

I am using following code for insert/update page views in memcached

if($memcached->is_valid_cache("visiors")) {
    $log_views = $memcached->get_cache("visiors");
    if(!is_array($log_views)) $log_views = array();
}
else {
    $log_views = array();
}
$log_views[] = array($page_id, $time, $other_Stuff);
$memcached->set_cache("visiors", $log_views, $cache_expire_time);

Following code retrieves the array from memcached, updates the X number of page views in db and sets the remaining page views in memcached

if($memcached->is_valid_cache("visiors")) {
   $log_views = $memcached->get_cache("visiors");
   if(is_array($log_views) && count($log_views) > 0) {
        $logs = array_slice($log_views, 0, $insert_limit);        
        $insert_array = array();
        foreach($logs as $log) {
            $insert_array[] = '('. $log[0]. ',' . $log[1] . ', NOW())';
        }
        $insert_sql = implode(',',$insert_array);
        if(mysql_query('INSERT SQL CODE')) {
            $memcached->set_cache("visiors", array_slice($log_views, $insert_limit), $cache_expire_time); //store new values
        }
    }
 }

The insert/update cause thread locking because I can see lots of script in waiting for their turn. I think I am losing page views during the update process. Any suggestions how to avoid memcached reading errors and make this code perfect?

  • 写回答

1条回答 默认 最新

  • douyun6399 2011-10-11 04:59
    关注

    You are likely running into a connection limit within memcached, your firewall, network, etc. We have a simple walk through on the most common scenarios: http://code.google.com/p/memcached/wiki/Timeouts

    There's no internal locking that would cause sets or gets to block for any amount of time.

    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办