mysql 分表 id_MySQL分表自增ID问题的解决方法

function get_next_autoincrement_waitlock($timeout = 60){

$count = $timeout > 0 ? $timeout : 60;

while($r->get("serial:lock")){

$count++;

sleep(1);

if ($count > 10)

return false;

}

return true;

}

function get_next_autoincrement($timeout = 60){

// first check if we are locked...

if (get_next_autoincrement_waitlock($timeout) == false)

return 0;

$id = $r->incr("serial");

if ( $id > 1 )

return $id;

// if ID == 1, we assume we do not have "serial" key...

// first we need to get lock.

if ($r->setnx("serial:lock"), 1){

$r->expire("serial:lock", 60 * 5);

// get max(id) from database.

$id = select_db_query("select max(id) from user_posts");

// or alternatively:

// select id from user_posts order by id desc limit 1

// increase it

$id++;

// update Redis key

$r->set("serial", $id);

// release the lock

$r->del("serial:lock");

return $id;

}

// can not get lock.

return 0;

}

$r = new Redis();

$r->connect("127.0.0.1", "6379");

$id = get_next_autoincrement();

if ($id){

$sql = "insert into user_posts(id,user,message)values($id,'$user','$message')"

$data = exec_db_query($sql);

}

热点聚合:

©CopyRight 2010-2020 EASCK.COM Inc All Rights Reserved. 易采站长站 版权所有

微信扫一扫

d7b2d4afa52a5197e0e332e6c15af170.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值