后端缓存管理
ci-userbase-service / build-and-push (push) Successful in 2m20s

This commit is contained in:
chenxing
2026-09-07 09:33:10 +08:00
parent 79b1d99db9
commit 1047d2387f
56 changed files with 3786 additions and 331 deletions
@@ -0,0 +1,32 @@
package com.yunhai.userbase.service.impl;
import com.yunhai.userbase.config.CacheNames;
import com.yunhai.userbase.model.GroupSnapshot;
import com.yunhai.userbase.repository.SysGroupRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
/**
* sys_group 全表快照读取服务({@code groupSnapshot} 缓存的唯一入口)。<br><br>
*
* 消除权限链路中按用户归属组循环重复执行的全表 {@code findAll()}:
* 一次 findAll 建索引(几十毫秒)后,同一快照内所有祖先链判定都是内存 Map 操作。<br>
* TTL 30min + GroupChangedEvent 事务提交后驱逐;{@code sync=true} 保证驱逐后并发读只重建一次。<br>
* <b>仅限只读路径使用</b>:组域写操作的事务内校验(环检测/边界校验)必须直查库,不能吃旧快照。
*/
@Service
public class GroupSnapshotService {
@Autowired
private SysGroupRepository sysGroupRepository;
/**
* 取全表快照(整体驱逐+重建,固定单 key)。
* 调用方通过本方法(经代理)访问才会命中缓存,禁止同类内部自调用。
*/
@Cacheable(value = CacheNames.GROUP_SNAPSHOT, key = "'all'", sync = true)
public GroupSnapshot snapshot() {
return new GroupSnapshot(sysGroupRepository.findAll());
}
}