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:
parent
c1fe54a8d4
commit
ab31ad3726
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 重新加载缓存(从数据库)
|
||||
*/
|
||||
|
|
|
|||
Loading…
Reference in New Issue