性能优化:N+1查询改批量、串行远程调用并发化、紫鸟索引巡检批量回写
This commit is contained in:
+51
@@ -1,6 +1,7 @@
|
||||
package com.nanri.aiimage.modules.ziniao.memory.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.modules.ziniao.memory.mapper.ZiniaoMemoryStoreMapper;
|
||||
import com.nanri.aiimage.modules.ziniao.memory.model.entity.ZiniaoMemoryStoreEntity;
|
||||
@@ -14,6 +15,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
@@ -181,6 +183,55 @@ public class ZiniaoMemoryStoreService {
|
||||
return deleted;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量标 STALE 回写(索引巡检):调用方已持有完整行(含 id),不再逐条 findOne,
|
||||
* 避免每轮刷新把数百行按"每个事务一条 SELECT + 一条 UPDATE"的方式打回数据库。
|
||||
* 仅 payload 内容发生变化的行走这里。
|
||||
*/
|
||||
@Transactional
|
||||
public int updateStaleMarks(List<ZiniaoMemoryStoreEntity> rows) {
|
||||
if (rows == null || rows.isEmpty()) {
|
||||
return 0;
|
||||
}
|
||||
int updated = 0;
|
||||
for (ZiniaoMemoryStoreEntity row : rows) {
|
||||
if (row == null || row.getId() == null) {
|
||||
continue;
|
||||
}
|
||||
updated += ziniaoMemoryStoreMapper.updateById(row);
|
||||
if (CACHE_TYPE_SHOP_INDEX_ENTRY.equals(row.getCacheType())) {
|
||||
log.info("[ziniao-shop-index-db] mark-stale cacheKey={} id={} companyNameSample={} expiresAt={} bytes={}",
|
||||
row.getCacheKey(), row.getId(), sampleCompanyNameForLog(row.getCompanyName()),
|
||||
row.getExpiresAt(), row.getPayloadJson() == null ? 0 : row.getPayloadJson().length());
|
||||
}
|
||||
}
|
||||
return updated;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量刷新过期时间(索引巡检):payload 未变化的行不重写 JSON,仅用一条 UPDATE 延长 TTL,
|
||||
* 保持"未过期行常驻表内"的原有语义。
|
||||
*/
|
||||
@Transactional
|
||||
public void touchExpiryBatch(List<ZiniaoMemoryStoreEntity> rows, Duration ttl) {
|
||||
if (rows == null || rows.isEmpty() || ttl == null || ttl.isZero() || ttl.isNegative()) {
|
||||
return;
|
||||
}
|
||||
List<Long> ids = new ArrayList<>();
|
||||
for (ZiniaoMemoryStoreEntity row : rows) {
|
||||
if (row != null && row.getId() != null) {
|
||||
ids.add(row.getId());
|
||||
}
|
||||
}
|
||||
if (ids.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
ziniaoMemoryStoreMapper.update(null, new LambdaUpdateWrapper<ZiniaoMemoryStoreEntity>()
|
||||
.in(ZiniaoMemoryStoreEntity::getId, ids)
|
||||
.set(ZiniaoMemoryStoreEntity::getExpiresAt, LocalDateTime.now().plusSeconds(ttl.getSeconds()))
|
||||
.set(ZiniaoMemoryStoreEntity::getUpdatedAt, LocalDateTime.now()));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public int deleteAllByType(String cacheType) {
|
||||
String normalizedType = normalizeRequired(cacheType, "cacheType 不能为空");
|
||||
|
||||
+27
-2
@@ -19,6 +19,7 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -590,10 +591,13 @@ public class ZiniaoShopIndexService {
|
||||
if (entities.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
List<ZiniaoMemoryStoreEntity> staleUpdates = new ArrayList<>();
|
||||
List<ZiniaoMemoryStoreEntity> expiryTouches = new ArrayList<>();
|
||||
int staleCount = 0;
|
||||
for (ZiniaoMemoryStoreEntity entity : entities) {
|
||||
String rowKey = entity.getCacheKey();
|
||||
if (activeCacheKeys.contains(rowKey)) {
|
||||
// 活跃行已由本轮 refresh 的 put() 刷新过 TTL,无需再碰
|
||||
continue;
|
||||
}
|
||||
ZiniaoShopIndexEntryDto existingEntry;
|
||||
@@ -629,10 +633,31 @@ public class ZiniaoShopIndexService {
|
||||
}
|
||||
existingEntry.setStatus(STATUS_STALE);
|
||||
existingEntry.setMessage("店铺索引已过期,请等待后台刷新");
|
||||
existingEntry.setLastRefreshedAt(now);
|
||||
ziniaoMemoryStoreService.put(ZiniaoMemoryStoreService.CACHE_TYPE_SHOP_INDEX_ENTRY, rowKey, existingEntry, resolveEntryTtl());
|
||||
// 不再更新 lastRefreshedAt:STALE 行查询路径不读该字段(仅 ACTIVE 行参与保鲜判断),
|
||||
// 跳过可让已标 STALE 且内容不变的行不再被逐轮全量重写。
|
||||
String newPayload;
|
||||
try {
|
||||
newPayload = objectMapper.writeValueAsString(existingEntry);
|
||||
} catch (Exception ex) {
|
||||
throw new BusinessException("写入紫鸟记忆存储失败", ex);
|
||||
}
|
||||
if (Objects.equals(newPayload, entity.getPayloadJson())) {
|
||||
// 内容未变化:仅延长 TTL,保持"未过期行常驻"语义,不重写 JSON
|
||||
expiryTouches.add(entity);
|
||||
continue;
|
||||
}
|
||||
entity.setPayloadJson(newPayload);
|
||||
entity.setExpiresAt(LocalDateTime.now().plus(resolveEntryTtl()));
|
||||
entity.setUpdatedAt(LocalDateTime.now());
|
||||
staleUpdates.add(entity);
|
||||
staleCount++;
|
||||
}
|
||||
if (!staleUpdates.isEmpty()) {
|
||||
ziniaoMemoryStoreService.updateStaleMarks(staleUpdates);
|
||||
}
|
||||
if (!expiryTouches.isEmpty()) {
|
||||
ziniaoMemoryStoreService.touchExpiryBatch(expiryTouches, resolveEntryTtl());
|
||||
}
|
||||
if (staleCount > 0) {
|
||||
log.info("[ziniao-index] marked stale shop_index rows count={}", staleCount);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user