fix: 修复 cacheManager bean 冲突问题

- 重命名 cacheManager bean 为 knowledgeGraphCacheManager 和 dataManagementCacheManager
- 更新所有引用处(@Cacheable、@Qualifier 注解)
- 添加 @Primary 注解到 knowledgeGraphCacheManager 避免多 bean 冲突
- 修复文件:
  - DataManagementConfig.java
  - RedisCacheConfig.java
  - GraphEntityService.java
  - GraphQueryService.java
  - GraphCacheService.java
  - CacheableIntegrationTest.java
This commit is contained in:
2026-02-23 16:18:32 +08:00
parent a5d8997c22
commit 1b2ed5335e
6 changed files with 18 additions and 11 deletions

View File

@@ -21,8 +21,8 @@ public class DataManagementConfig {
/** /**
* 缓存管理器 * 缓存管理器
*/ */
@Bean @Bean("dataManagementCacheManager")
public CacheManager cacheManager() { public CacheManager dataManagementCacheManager() {
return new ConcurrentMapCacheManager("datasets", "datasetFiles", "tags"); return new ConcurrentMapCacheManager("datasets", "datasetFiles", "tags");
} }

View File

@@ -63,7 +63,8 @@ public class GraphEntityService {
@Cacheable(value = RedisCacheConfig.CACHE_ENTITIES, @Cacheable(value = RedisCacheConfig.CACHE_ENTITIES,
key = "T(com.datamate.knowledgegraph.infrastructure.cache.GraphCacheService).cacheKey(#graphId, #entityId)", key = "T(com.datamate.knowledgegraph.infrastructure.cache.GraphCacheService).cacheKey(#graphId, #entityId)",
unless = "#result == null") unless = "#result == null",
cacheManager = "knowledgeGraphCacheManager")
public GraphEntity getEntity(String graphId, String entityId) { public GraphEntity getEntity(String graphId, String entityId) {
validateGraphId(graphId); validateGraphId(graphId);
return entityRepository.findByIdAndGraphId(entityId, graphId) return entityRepository.findByIdAndGraphId(entityId, graphId)
@@ -71,7 +72,8 @@ public class GraphEntityService {
} }
@Cacheable(value = RedisCacheConfig.CACHE_ENTITIES, @Cacheable(value = RedisCacheConfig.CACHE_ENTITIES,
key = "T(com.datamate.knowledgegraph.infrastructure.cache.GraphCacheService).cacheKey(#graphId, 'list')") key = "T(com.datamate.knowledgegraph.infrastructure.cache.GraphCacheService).cacheKey(#graphId, 'list')",
cacheManager = "knowledgeGraphCacheManager")
public List<GraphEntity> listEntities(String graphId) { public List<GraphEntity> listEntities(String graphId) {
validateGraphId(graphId); validateGraphId(graphId);
return entityRepository.findByGraphId(graphId); return entityRepository.findByGraphId(graphId);

View File

@@ -73,7 +73,8 @@ public class GraphQueryService {
* @param limit 返回节点数上限 * @param limit 返回节点数上限
*/ */
@Cacheable(value = RedisCacheConfig.CACHE_QUERIES, @Cacheable(value = RedisCacheConfig.CACHE_QUERIES,
key = "T(com.datamate.knowledgegraph.infrastructure.cache.GraphCacheService).cacheKey(#graphId, #entityId, #depth, #limit, @resourceAccessService.resolveOwnerFilterUserId(), @resourceAccessService.canViewConfidential())") key = "T(com.datamate.knowledgegraph.infrastructure.cache.GraphCacheService).cacheKey(#graphId, #entityId, #depth, #limit, @resourceAccessService.resolveOwnerFilterUserId(), @resourceAccessService.canViewConfidential())",
cacheManager = "knowledgeGraphCacheManager")
public SubgraphVO getNeighborGraph(String graphId, String entityId, int depth, int limit) { public SubgraphVO getNeighborGraph(String graphId, String entityId, int depth, int limit) {
validateGraphId(graphId); validateGraphId(graphId);
String filterUserId = resolveOwnerFilter(); String filterUserId = resolveOwnerFilter();
@@ -573,7 +574,8 @@ public class GraphQueryService {
* @param query 搜索关键词(支持 Lucene 查询语法) * @param query 搜索关键词(支持 Lucene 查询语法)
*/ */
@Cacheable(value = RedisCacheConfig.CACHE_SEARCH, @Cacheable(value = RedisCacheConfig.CACHE_SEARCH,
key = "T(com.datamate.knowledgegraph.infrastructure.cache.GraphCacheService).cacheKey(#graphId, #query, #page, #size, @resourceAccessService.resolveOwnerFilterUserId(), @resourceAccessService.canViewConfidential())") key = "T(com.datamate.knowledgegraph.infrastructure.cache.GraphCacheService).cacheKey(#graphId, #query, #page, #size, @resourceAccessService.resolveOwnerFilterUserId(), @resourceAccessService.canViewConfidential())",
cacheManager = "knowledgeGraphCacheManager")
public PagedResponse<SearchHitVO> fulltextSearch(String graphId, String query, int page, int size) { public PagedResponse<SearchHitVO> fulltextSearch(String graphId, String query, int page, int size) {
validateGraphId(graphId); validateGraphId(graphId);
String filterUserId = resolveOwnerFilter(); String filterUserId = resolveOwnerFilter();

View File

@@ -2,6 +2,7 @@ package com.datamate.knowledgegraph.infrastructure.cache;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.Cache; import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager; import org.springframework.cache.CacheManager;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
@@ -28,7 +29,7 @@ public class GraphCacheService {
private final CacheManager cacheManager; private final CacheManager cacheManager;
private StringRedisTemplate redisTemplate; private StringRedisTemplate redisTemplate;
public GraphCacheService(CacheManager cacheManager) { public GraphCacheService(@Qualifier("knowledgeGraphCacheManager") CacheManager cacheManager) {
this.cacheManager = cacheManager; this.cacheManager = cacheManager;
} }

View File

@@ -7,6 +7,7 @@ import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisConnectionFactory;
@@ -43,8 +44,9 @@ public class RedisCacheConfig {
/** 搜索缓存:全文搜索结果 */ /** 搜索缓存:全文搜索结果 */
public static final String CACHE_SEARCH = "kg:search"; public static final String CACHE_SEARCH = "kg:search";
@Bean @Primary
public CacheManager cacheManager( @Bean("knowledgeGraphCacheManager")
public CacheManager knowledgeGraphCacheManager(
RedisConnectionFactory connectionFactory, RedisConnectionFactory connectionFactory,
KnowledgeGraphProperties properties KnowledgeGraphProperties properties
) { ) {

View File

@@ -47,8 +47,8 @@ class CacheableIntegrationTest {
@EnableCaching @EnableCaching
static class Config { static class Config {
@Bean @Bean("knowledgeGraphCacheManager")
CacheManager cacheManager() { CacheManager knowledgeGraphCacheManager() {
return new ConcurrentMapCacheManager( return new ConcurrentMapCacheManager(
RedisCacheConfig.CACHE_ENTITIES, RedisCacheConfig.CACHE_ENTITIES,
RedisCacheConfig.CACHE_QUERIES, RedisCacheConfig.CACHE_QUERIES,