feat: AGV点位锁与缓存迁移 + 工位终端去离线队列 + WMS出入库增强
【MES】 - AGV点位/线路点位/AGV任务/工单 ent schema 重构(全量重生成 ent 代码) - 新增派车锁 agv_point_lock.go:目标点位占用即拒车,JBT21 库房接驳台卸货后保持锁定待库管手动解锁 - 新增进程内 TTL 缓存 cache.go,由 Redis 看板缓存迁移至内存(singleflight 防击穿,去除外部依赖) - AGV 三段链安全机制、车辆状态/接驳台左右互换、补料损耗闭环完善 - 来料检/工序卡/工单/质检/日排产/AGV 前端与帮助文档更新 - 新增 migrations/、sql/ 目录 【WMS】 - 出库、Excel 导出、物料、路由处理增强 - 配置与 DB 初始化调整;基础数据/成品入库/入库前端调整 【工位终端】 - 移除离线同步队列(syncer 包、sync.go、SyncQueueDialog.vue),改为实时直连上报 MES(断网即失败报错),以本人身份操作 - 本地 SQLite 仅保留操作日志 + 内置账号 + 登录态;保留空闲自动登出 【文档】 - 删除 AGV海康litev1.5.md;案例.md 更新
This commit is contained in:
@@ -2,7 +2,6 @@ package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"bj_power_mes/internal/svc"
|
||||
@@ -26,78 +25,16 @@ func (s *Service) notifyDashboard() {
|
||||
}
|
||||
}
|
||||
|
||||
// cachedTyped 泛型版缓存:与 cached 同为 TTL + SETNX 防击穿,
|
||||
// 区别是返回强类型,调用方不必再做类型断言(看板快照等结构化数据用这个)。
|
||||
// cachedTyped 看板进程内缓存(2026-11-05 由 Redis 等价迁移):
|
||||
// TTL + singleflight 防击穿,返回强类型,调用方不必再做类型断言(看板快照等结构化数据用这个)。
|
||||
// 原 Redis 版的零调用死代码 cached(any) 已一并删除。
|
||||
func cachedTyped[T any](ctx context.Context, s *Service, key string, ttl int, build func() (T, error)) (T, error) {
|
||||
if s.ctx.RedisClient == nil {
|
||||
v, err := dashboardCache.getOrBuild(key, time.Duration(ttl)*time.Second, func() (any, error) {
|
||||
return build()
|
||||
})
|
||||
if err != nil {
|
||||
var zero T
|
||||
return zero, err
|
||||
}
|
||||
if v, err := s.ctx.RedisClient.Get(key); err == nil && v != "" {
|
||||
var out T
|
||||
if json.Unmarshal([]byte(v), &out) == nil {
|
||||
return out, nil
|
||||
}
|
||||
}
|
||||
|
||||
lockKey := key + ":lock"
|
||||
ok, _ := s.ctx.RedisClient.SetnxEx(lockKey, "1", 10)
|
||||
if !ok {
|
||||
for i := 0; i < 10; i++ {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
if v, err := s.ctx.RedisClient.Get(key); err == nil && v != "" {
|
||||
var out T
|
||||
if json.Unmarshal([]byte(v), &out) == nil {
|
||||
return out, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return build()
|
||||
}
|
||||
defer func() { _, _ = s.ctx.RedisClient.Del(lockKey) }()
|
||||
|
||||
data, err := build()
|
||||
if err == nil {
|
||||
if b, e := json.Marshal(data); e == nil {
|
||||
_ = s.ctx.RedisClient.Setex(key, string(b), ttl)
|
||||
}
|
||||
}
|
||||
return data, err
|
||||
}
|
||||
|
||||
// cached 看板缓存:TTL + 防击穿(SETNX 锁),同一 key 同时仅一个打到 DB
|
||||
func (s *Service) cached(ctx context.Context, key string, ttl int, build func() (any, error)) (any, error) {
|
||||
if s.ctx.RedisClient == nil {
|
||||
return build()
|
||||
}
|
||||
if v, err := s.ctx.RedisClient.Get(key); err == nil && v != "" {
|
||||
var out any
|
||||
if json.Unmarshal([]byte(v), &out) == nil {
|
||||
return out, nil
|
||||
}
|
||||
}
|
||||
|
||||
lockKey := key + ":lock"
|
||||
ok, _ := s.ctx.RedisClient.SetnxEx(lockKey, "1", 10)
|
||||
if !ok {
|
||||
// 有人正在重建缓存:短暂等待后重读
|
||||
for i := 0; i < 10; i++ {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
if v, err := s.ctx.RedisClient.Get(key); err == nil && v != "" {
|
||||
var out any
|
||||
if json.Unmarshal([]byte(v), &out) == nil {
|
||||
return out, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return build()
|
||||
}
|
||||
defer func() { _, _ = s.ctx.RedisClient.Del(lockKey) }()
|
||||
|
||||
data, err := build()
|
||||
if err == nil {
|
||||
if b, e := json.Marshal(data); e == nil {
|
||||
_ = s.ctx.RedisClient.Setex(key, string(b), ttl)
|
||||
}
|
||||
}
|
||||
return data, err
|
||||
return v.(T), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user