From ab31ad3726a2e8525a82e55cf3ab7c41e4454174 Mon Sep 17 00:00:00 2001 From: hailin Date: Mon, 15 Dec 2025 23:13:04 -0800 Subject: [PATCH] =?UTF-8?q?feat(blockchain):=20=E6=B7=BB=E5=8A=A0=20Redis?= =?UTF-8?q?=20=E7=BC=93=E5=AD=98=E8=87=AA=E5=8A=A8=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 扫描区块时检测缓存是否为空,如果为空则自动从数据库重新加载地址缓存, 避免因 Redis 数据丢失导致充值漏检。 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- .../application/services/deposit-detection.service.ts | 9 +++++++++ .../src/infrastructure/redis/address-cache.service.ts | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/backend/services/blockchain-service/src/application/services/deposit-detection.service.ts b/backend/services/blockchain-service/src/application/services/deposit-detection.service.ts index 9417faaf..7fa7b05f 100644 --- a/backend/services/blockchain-service/src/application/services/deposit-detection.service.ts +++ b/backend/services/blockchain-service/src/application/services/deposit-detection.service.ts @@ -94,6 +94,15 @@ export class DepositDetectionService implements OnModuleInit { * 扫描单条链 */ private async scanChain(chainType: ChainType): Promise { + // 检查缓存是否为空,如果为空则自动从数据库重新加载 + 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); diff --git a/backend/services/blockchain-service/src/infrastructure/redis/address-cache.service.ts b/backend/services/blockchain-service/src/infrastructure/redis/address-cache.service.ts index ef9386c8..d2d7dccd 100644 --- a/backend/services/blockchain-service/src/infrastructure/redis/address-cache.service.ts +++ b/backend/services/blockchain-service/src/infrastructure/redis/address-cache.service.ts @@ -66,6 +66,15 @@ export class AddressCacheService implements OnModuleInit { return this.redis.smembers(key); } + /** + * 获取监控地址数量 + */ + async getCount(chainType: ChainType): Promise { + const key = this.getCacheKey(chainType); + const client = this.redis.getClient(); + return client.scard(key); + } + /** * 重新加载缓存(从数据库) */