2026-08-28 15:06:01 +08:00
|
|
|
package logic
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"bj_power_mes/internal/svc"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Service 业务逻辑服务(handler 薄层调用)
|
|
|
|
|
type Service struct {
|
|
|
|
|
ctx *svc.ServiceContext
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func New(s *svc.ServiceContext) *Service {
|
|
|
|
|
return &Service{ctx: s}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-04 17:31:18 +08:00
|
|
|
// notifyDashboard 生产业务数据落库成功后广播看板刷新事件。
|
|
|
|
|
// SSE 事件只做"数据变了"的通知(空载荷),大屏收到后自行拉取最新快照,
|
|
|
|
|
// 避免把整份快照塞进事件流。无订阅客户端时 Publish 为空操作,零开销。
|
|
|
|
|
func (s *Service) notifyDashboard() {
|
|
|
|
|
if s.ctx.SSE != nil {
|
|
|
|
|
s.ctx.SSE.Publish("dashboard.updated", "{}")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-09 18:58:47 +08:00
|
|
|
// cachedTyped 看板进程内缓存:
|
2026-11-07 15:06:00 +08:00
|
|
|
// TTL + singleflight 防击穿,返回强类型,调用方不必再做类型断言(看板快照等结构化数据用这个)。
|
2026-11-09 18:58:47 +08:00
|
|
|
// 零调用的旧 cached(any) 死代码已一并删除。
|
2026-09-04 14:18:53 +08:00
|
|
|
func cachedTyped[T any](ctx context.Context, s *Service, key string, ttl int, build func() (T, error)) (T, error) {
|
2026-11-07 15:06:00 +08:00
|
|
|
v, err := dashboardCache.getOrBuild(key, time.Duration(ttl)*time.Second, func() (any, error) {
|
2026-09-04 14:18:53 +08:00
|
|
|
return build()
|
2026-11-07 15:06:00 +08:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
var zero T
|
|
|
|
|
return zero, err
|
2026-09-04 14:18:53 +08:00
|
|
|
}
|
2026-11-07 15:06:00 +08:00
|
|
|
return v.(T), nil
|
2026-08-28 15:06:01 +08:00
|
|
|
}
|