- 主体状态缓存(Redis 优先、进程内兜底):设备心跳与上报判断不再查库,续期/停用后主动失效 - 上报写接口按缓存校验期限:过期 403 subject_expired、停用 subject_disabled、主体缺失 subject_missing - 心跳与 /api/scanner/me 返回 subject 状态,桌面端据此暂停推送并在续期后自动补传积压 - 新增续期订单(subject_orders)与接口,支付回调 POST /api/pay/notify 幂等、支持 HMAC 签名, 另提供内部令牌模拟支付用于联调 - 顺带提交工作区中已完成的「登录协议」后端改动(agreement):与本次改动共用 router.js / config.default.js Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
105 lines
3.3 KiB
JavaScript
105 lines
3.3 KiB
JavaScript
'use strict';
|
||
|
||
const { Service } = require('egg');
|
||
|
||
/**
|
||
* 进程内兜底缓存:Redis 不可用(或单测没起 Redis)时,也保证高频读路径不查库。
|
||
* key = subjectId,value = { value, expiresAt }
|
||
*/
|
||
const localCache = new Map();
|
||
const LOCAL_MAX = 2000;
|
||
|
||
/**
|
||
* 主体状态缓存(**只给高频读路径用**:设备心跳、上报过期判断)
|
||
*
|
||
* - 只缓存判断「过期 / 停用」需要的几个字段,不缓存成员关系(角色校验仍走数据库);
|
||
* - Redis 命中优先(多实例共享),Redis 不可用时退化为进程内 TTL 缓存;
|
||
* - 续期 / 改名 / 停用后必须调用 `invalidate()`,否则最多有一个 TTL 的延迟。
|
||
*/
|
||
class SubjectStateService extends Service {
|
||
get ttlSeconds() {
|
||
const cfg = this.config.subject || {};
|
||
const ttl = Number(cfg.stateCacheTtl);
|
||
return Number.isFinite(ttl) && ttl > 0 ? ttl : 30;
|
||
}
|
||
|
||
get Subject() {
|
||
return this.app.model.Subject;
|
||
}
|
||
|
||
cacheKey(subjectId) {
|
||
return `subject:state:${Number(subjectId)}`;
|
||
}
|
||
|
||
/**
|
||
* 读主体状态(缓存命中不回源);主体不存在返回 null
|
||
* @param {number|string} subjectId
|
||
* @return {Promise<object|null>}
|
||
*/
|
||
async get(subjectId) {
|
||
const id = Number(subjectId);
|
||
if (!Number.isInteger(id) || id <= 0) return null;
|
||
|
||
const cachedLocal = localCache.get(id);
|
||
if (cachedLocal && cachedLocal.expiresAt > Date.now()) return decorate(cachedLocal.value);
|
||
|
||
const cached = await this.ctx.service.cache.get(this.cacheKey(id));
|
||
if (cached) {
|
||
rememberLocal(id, cached, this.ttlSeconds);
|
||
return decorate(cached);
|
||
}
|
||
|
||
const row = await this.Subject.findByPk(id);
|
||
if (!row) return null;
|
||
|
||
const value = {
|
||
id: Number(row.id),
|
||
name: row.name,
|
||
adminUserId: Number(row.adminUserId),
|
||
status: Number(row.status),
|
||
expireAt: row.expireAt ? new Date(row.expireAt).toISOString() : null,
|
||
};
|
||
|
||
await this.ctx.service.cache.set(this.cacheKey(id), value, this.ttlSeconds);
|
||
rememberLocal(id, value, this.ttlSeconds);
|
||
return decorate(value);
|
||
}
|
||
|
||
/** 续期 / 改名 / 停用 / 删除之后调用(写库成功后),避免继续读到旧状态 */
|
||
async invalidate(subjectId) {
|
||
const id = Number(subjectId);
|
||
if (!id) return;
|
||
localCache.delete(id);
|
||
await this.ctx.service.cache.del(this.cacheKey(id));
|
||
}
|
||
|
||
/** 单测用:清空进程内缓存,避免用例之间互相污染 */
|
||
clearLocalCache() {
|
||
localCache.clear();
|
||
}
|
||
}
|
||
|
||
/** 每次读取都按服务器当前时间派生 expired / remainingSeconds(缓存里只存静态字段) */
|
||
function decorate(value) {
|
||
const expireMs = value.expireAt ? new Date(value.expireAt).getTime() : null;
|
||
const now = Date.now();
|
||
return Object.assign({}, value, {
|
||
expired: expireMs !== null && expireMs <= now,
|
||
disabled: Number(value.status) !== 1,
|
||
remainingSeconds: expireMs === null ? null : Math.max(0, Math.floor((expireMs - now) / 1000)),
|
||
});
|
||
}
|
||
|
||
function rememberLocal(id, value, ttlSeconds) {
|
||
if (localCache.size >= LOCAL_MAX) {
|
||
const now = Date.now();
|
||
for (const [ key, item ] of localCache) {
|
||
if (item.expiresAt <= now) localCache.delete(key);
|
||
}
|
||
if (localCache.size >= LOCAL_MAX) localCache.clear();
|
||
}
|
||
localCache.set(id, { value, expiresAt: Date.now() + ttlSeconds * 1000 });
|
||
}
|
||
|
||
module.exports = SubjectStateService;
|