缓存工具 CacheUtils 增加 computeIfAbsent 简化方法

This commit is contained in:
thinkgem
2025-10-10 11:12:59 +08:00
parent 92d4161294
commit 3dd0bdcd62
6 changed files with 75 additions and 76 deletions

View File

@@ -28,8 +28,7 @@ public class CacheChatMemoryRepository implements ChatMemoryRepository {
@Override
public @NotNull List<Message> findByConversationId(@NotNull String conversationId) {
List<Message> all = CacheUtils.get(CMS_CHAT_MSG_CACHE, conversationId);
return all != null ? all : List.of();
return CacheUtils.computeIfAbsent(CMS_CHAT_MSG_CACHE, conversationId, k -> List.of());
}
@Override

View File

@@ -85,11 +85,7 @@ public class CmsAiChatService extends BaseService {
}
public Map<String, Map<String, Object>> getChatCacheMap() {
Map<String, Map<String, Object>> cache = CacheUtils.get(CMS_CHAT_CACHE, getChatCacheKey());
if (cache == null) {
cache = MapUtils.newHashMap();
}
return cache;
return CacheUtils.computeIfAbsent(CMS_CHAT_CACHE, getChatCacheKey(), k -> MapUtils.newHashMap());
}
/**