Files
UserBaseService/src/main/java/com/yunhai/userbase/service/impl/GroupSnapshotService.java
T
chenxing 1047d2387f
ci-userbase-service / build-and-push (push) Successful in 2m20s
后端缓存管理
2026-09-07 09:33:10 +08:00

33 lines
1.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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());
}
}