/** * 获取一个自动分配的序号(注册服务器用) * * @return int|false */ public static function getNewHostId($group = 'default') { if (!self::$isRegisterServer) { throw new \Exception('function Host::getNewHostId() only run by register server.'); } while (true) { # 获取一个自增ID $id = self::$groupIdTable->incr($group, 'id'); if (false === $id) { return false; } else { $id--; if (self::$table->exist("{$group}_{$id}")) { # 已经存在 continue; } else { return $id; } } } return false; }
/** * 原子自减操作,可用于整形或浮点型列 * * @param $key * @param $column * @param $decrby * @return bool|int */ function decr($key, $column, $decrby = 1) { if ($this->_link) { $rs = parent::incr($key, $column, $decrby); if (false === $rs) { return false; } if (false === $this->_driverSet($key, [$column => $rs])) { # 重试 usleep(300); $this->_driverSet($key, [$column => $rs]); } return $rs; } else { return parent::decr($key, $column, $decrby); } }