refactor(time):彻底重构time类型。

This commit is contained in:
徐涛
2023-06-05 22:09:42 +08:00
parent 1fd5e7b9aa
commit 8aa3a054b0
10 changed files with 79 additions and 73 deletions

View File

@@ -4,9 +4,9 @@ import (
"electricity_bill_calc/config"
"electricity_bill_calc/global"
"electricity_bill_calc/logger"
"electricity_bill_calc/tools/time"
"electricity_bill_calc/types"
"fmt"
stdTime "time"
"time"
"github.com/rueian/rueidis"
"github.com/samber/lo"
@@ -18,7 +18,7 @@ var log = logger.Named("Algorithm", "Unique Serial")
// 在防止服务器时间回拨的情况下,生成一个用于生成唯一编号的精简时间戳,该时间戳的起始时间为`2022-02-22 22:22:22.000000 +0800`,时间戳精确到秒。
func generateTimestamp() int64 {
for {
timestamp := time.Timestamp()
timestamp := types.Timestamp()
cmds := make(rueidis.Commands, 0, 2)
cmds = append(cmds, global.Rd.B().Set().Key("LAST_TIMESTAMP").Value(fmt.Sprintf("%d", timestamp)).Nx().ExSeconds(1).Build())
cmds = append(cmds, global.Rd.B().Get().Key("LAST_TIMESTAMP").Build())
@@ -26,14 +26,14 @@ func generateTimestamp() int64 {
store_res, err := lo.Last(results)
if err != nil {
log.Error("从Redis缓存中获取上一次的时间戳失败。", zap.Error(err))
stdTime.Sleep(1 * stdTime.Second)
time.Sleep(1 * time.Second)
continue
}
if stored_timestamp, err := store_res.AsInt64(); err == nil && timestamp >= stored_timestamp {
return timestamp
} else {
log.Error("转换从Redis缓存中获取的时间戳失败。", zap.Error(err))
stdTime.Sleep(1 * stdTime.Second)
time.Sleep(1 * time.Second)
continue
}
}
@@ -50,14 +50,14 @@ func GenerateUniqueSerial() int64 {
store_res, err := lo.Last(results)
if err != nil {
log.Error("从Redis缓存中获取上一次的序列号失败。", zap.Error(err))
stdTime.Sleep(1 * stdTime.Second)
time.Sleep(1 * time.Second)
continue
}
if stored_serial, err := store_res.AsInt64(); err == nil {
return (timestamp << 20) | ((config.ServiceSettings.HostSerial & 0xffff) << 16) | (stored_serial & 0xffff_ffff)
} else {
log.Error("转换从Redis缓存中获取的序列号失败。", zap.Error(err))
stdTime.Sleep(1 * stdTime.Second)
time.Sleep(1 * time.Second)
continue
}
}