简单的订单号创建(php):
function generateOrderSn()
{
$yearCode = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K"];
//生成规则年(转换成一个大写字母来缩短长度)+月(转换为十六进制数)+日+当前时间戳的后
//几位+当前的微秒数(小数点后面的5位)+一个两位的随机year的算法
$year = $yearCode[date("Y") - 2018]; //用A表示2018年等,目的是缩短订单号
$month = strtoupper(dechex(date("m")));//十六进制
$day = date("d");
$sec = substr(time(), -5); //取时间戳的后5位,因为前面的大至不会改变
[$micr, $second] = explode("", microtime());
$microsec = substr($micr, 2, 5);//取得微秒数小数点后面的5位数
$rand = random_int(10, 99);
$orderSn = $year . $month . $day . $sec . $microsec . $rand . uniqid('', true);
return $orderSn;
}
如果在高并发情况下不排除可能重复的情况;(可能会出现订单号重复)
高并发情况下,可以使用一些成熟的算法如:
php雪花算法实现:(这篇文章不错)
雪花算法是Twitter公司发明的一种算法,
主要目的是解决在分布式环境下,ID怎样生成的问题
1,分布式ID生成规则硬性要求:全局唯一:不能出现重复的ID号,既然是唯一标识,这是最基本的要求。趋势递增:MySQL中InnoDB引擎使用的是聚集索引。多数RDBMS使用Btree的数据结构来存储索引数据,在主键的选择上尽量选择有序的主键保证写入性能。单调递增:保证下一个ID号一定大于上一个
这样就不怕了😝😝😝😝😝
Snowflake的核心思想是将64bit的二进制数字分成若干部分,每一部分都存储有特定含义的数据,比如说时间戳、机器ID、序列号等等,最终生成全局唯一的有序ID。
<?php
class SnowFlake
{
const TWEPOCH = 1288834974657; // 时间起始标记点,作为基准,一般取系统的最近时间(一旦确定不能变动)
const WORKER_ID_BITS = 5; // 机器标识位数
const DATACENTER_ID_BITS = 5; // 数据中心标识位数
const SEQUENCE_BITS = 12; // 毫秒内自增位
private $workerId; // 工作机器ID
private $datacenterId; // 数据中心ID
private $sequence; // 毫秒内序列
private $maxWorkerId = -1 ^ (-1 << self::WORKER_ID_BITS); // 机器ID最大值
private $maxDatacenterId = -1 ^ (-1 << self::DATACENTER_ID_BITS); // 数据中心ID最大值
private $workerIdShift = self::SEQUENCE_BITS; // 机器ID偏左移位数
private $datacenterIdShift = self::SEQUENCE_BITS + self::WORKER_ID_BITS; // 数据中心ID左移位数
private $timestampLeftShift = self::SEQUENCE_BITS + self::WORKER_ID_BITS + self::DATACENTER_ID_BITS; // 时间毫秒左移位数
private $sequenceMask = -1 ^ (-1 << self::SEQUENCE_BITS); // 生成序列的掩码
private $lastTimestamp = -1; // 上次生产id时间戳
public function __construct($workerId, $datacenterId, $sequence = 0)
{
if ($workerId > $this->maxWorkerId || $workerId < 0) {
throw new Exception("worker Id can't be greater than {$this->maxWorkerId} or less than 0");
}
if ($datacenterId > $this->maxDatacenterId || $datacenterId < 0) {
throw new Exception("datacenter Id can't be greater than {$this->maxDatacenterId} or less than 0");
}
$this->workerId = $workerId;
$this->datacenterId = $datacenterId;
$this->sequence = $sequence;
}
public function nextId()
{
$timestamp = $this->timeGen();
if ($timestamp < $this->lastTimestamp) {
$diffTimestamp = bcsub($this->lastTimestamp, $timestamp);
throw new Exception("Clock moved backwards. Refusing to generate id for {$diffTimestamp} milliseconds");
}
if ($this->lastTimestamp == $timestamp) {
$this->sequence = ($this->sequence + 1) & $this->sequenceMask;
if (0 == $this->sequence) {
$timestamp = $this->tilNextMillis($this->lastTimestamp);
}
} else {
$this->sequence = 0;
}
$this->lastTimestamp = $timestamp;
/*$gmpTimestamp = gmp_init($this->leftShift(bcsub($timestamp, self::TWEPOCH), $this->timestampLeftShift));
$gmpDatacenterId = gmp_init($this->leftShift($this->datacenterId, $this->datacenterIdShift));
$gmpWorkerId = gmp_init($this->leftShift($this->workerId, $this->workerIdShift));
$gmpSequence = gmp_init($this->sequence);
return gmp_strval(gmp_or(gmp_or(gmp_or($gmpTimestamp, $gmpDatacenterId), $gmpWorkerId), $gmpSequence));*/
return (($timestamp - self::TWEPOCH) << $this->timestampLeftShift) |
($this->datacenterId << $this->datacenterIdShift) |
($this->workerId << $this->workerIdShift) |
$this->sequence;
}
protected function tilNextMillis($lastTimestamp)
{
$timestamp = $this->timeGen();
while ($timestamp <= $lastTimestamp) {
$timestamp = $this->timeGen();
}
return $timestamp;
}
protected function timeGen()
{
return floor(microtime(true) * 1000);
}
// 左移 <<
protected function leftShift($a, $b)
{
return bcmul($a, bcpow(2, $b));
}
}
补充个小点:
哈希函数也可以称为散列函数,哈希函数的功能只是将你的目标key通过一种映射方法,也可以说是一种函数运算f,最后得到你目标的hashValue = f(key),这里的函数f就称为哈希函数/散列函数。
数组的底层就是哈希散列;
数组(Array):一种线性表数据结构,用一组连续的内存空间,来存储一组具有相同类型的数据。
数组找出重复值和不重复值(小点):
//用hash/桶的思路$res=[];
foreach ($dataas$item) {
if (array_key_exists($item, $res)) {
++$res[$item];
} else {
$res[$item] = 1;
}
}
foreach ($resas$k => $v) {
if ($v == 1) {
echo $k;
}
}