Skip to content

Commit

Permalink
重构seq服务,解决并发id重复问题
Browse files Browse the repository at this point in the history
  • Loading branch information
twinh committed Jan 30, 2017
1 parent ad8c1a2 commit f8c0be5
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Migration/V20161116144532CreateSeqTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public function up()
$this->schema->table('seq')
->bigInt('id')->autoIncrement()
->primary('id')
->tinyInt('stub')
->unique('stub')
->exec();

$this->db->insert('seq', ['id' => 1]);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Service/Seq.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __invoke($offset = 100)
return $this->redis->incr($name, $offset);

case 'db':
$stmt = $this->db->query("UPDATE $name SET id = id + $offset;SELECT id FROM $name");
$stmt = $this->db->query("REPLACE INTO $name (stub) VALUES (1);SELECT LAST_INSERT_ID();");
$stmt->nextRowset();

return (int) $stmt->fetchColumn();
Expand Down

0 comments on commit f8c0be5

Please sign in to comment.