feat(blockchain): 添加 Redis 缓存自动恢复机制

扫描区块时检测缓存是否为空,如果为空则自动从数据库重新加载地址缓存,
避免因 Redis 数据丢失导致充值漏检。

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2025-12-15 23:13:04 -08:00
parent c1fe54a8d4
commit ab31ad3726
2 changed files with 18 additions and 0 deletions

View File

@ -94,6 +94,15 @@ export class DepositDetectionService implements OnModuleInit {
*
*/
private async scanChain(chainType: ChainType): Promise<void> {
// 检查缓存是否为空,如果为空则自动从数据库重新加载
const cacheCount = await this.addressCache.getCount(chainType);
if (cacheCount === 0) {
this.logger.warn(`Address cache empty for ${chainType}, reloading from database...`);
const addresses = await this.monitoredAddressRepo.getAllActiveAddresses(chainType);
await this.addressCache.reloadCache(chainType, addresses);
this.logger.log(`Reloaded ${addresses.length} addresses for ${chainType} into cache`);
}
// 获取上次扫描位置
let lastBlock = await this.checkpointRepo.getLastScannedBlock(chainType);

View File

@ -66,6 +66,15 @@ export class AddressCacheService implements OnModuleInit {
return this.redis.smembers(key);
}
/**
*
*/
async getCount(chainType: ChainType): Promise<number> {
const key = this.getCacheKey(chainType);
const client = this.redis.getClient();
return client.scard(key);
}
/**
*
*/