-
Notifications
You must be signed in to change notification settings - Fork 10
feat: redis #130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: redis #130
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This pull request introduces comprehensive Redis-based caching to the Tari Explorer backend, replacing direct gRPC calls with cached data retrieval for improved performance. The implementation includes distributed locking for background updates and standardized cache key management across the application.
- Implements Redis caching infrastructure with distributed locking for coordinated background updates
- Refactors route handlers to use cached data with gRPC fallbacks when cache is unavailable
- Adds health monitoring for Redis connectivity and updates CI/CD configuration
Reviewed Changes
Copilot reviewed 14 out of 16 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| utils/updater.ts | Enhanced background updater with distributed locking and Redis cache population |
| utils/redisClient.ts | New Redis client singleton with connection management and error handling |
| utils/distributedLock.ts | New distributed locking mechanism using Redis for coordinating background processes |
| utils/cacheService.ts | New centralized caching service with Redis operations and BigInt serialization |
| utils/cacheKeys.ts | Standardized cache key definitions and lock key management |
| routes/miners.ts | Updated to use cached network stats with gRPC fallback |
| routes/mempool.ts | Updated to use cached mempool data with gRPC fallback |
| routes/index.ts | Simplified cache header handling and improved error flow |
| routes/healthz.ts | Enhanced health check to include Redis connectivity status |
| routes/blocks.ts | Updated to use cached mining stats and tip data |
| package.json | Added Redis dependencies and ESLint compatibility package |
| index.ts | Added Redis client initialization on server startup |
| eslint.config.js | Updated to import .gitignore patterns for better linting |
| .github/workflows/build_docker.yml | Temporarily disabled ARM64 builds and simplified manifest annotations |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| if (connecting) { | ||
| // If we're in the middle of connecting, wait a bit and return the client | ||
| // ioredis handles queuing commands until connection is ready | ||
| return client!; |
Copilot
AI
Aug 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using non-null assertion operator on client when it might be null. If connecting is true but client is still null, this will throw a runtime error. The logic should ensure client exists before returning it.
| return client!; | |
| // If we're in the middle of connecting, we cannot return a client yet | |
| // because it has not been created. Throw an explicit error. | |
| throw new Error('Redis client is still connecting. Please retry shortly.'); |
| for (let j = 0; j < (mempool[i]?.transaction?.body?.kernels?.length || 0); j++) { | ||
| sum += mempool[i]?.transaction?.body?.kernels[j]?.fee || 0n; | ||
| } | ||
| (mempool[i]?.transaction?.body as any).total_fees = sum; |
Copilot
AI
Aug 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using any type assertion bypasses type safety. Consider defining a proper type or interface for the transaction body with the total_fees property to maintain type safety.
| (mempool[i]?.transaction?.body as any).total_fees = sum; | |
| (mempool[i]?.transaction?.body as TransactionBodyWithTotalFees).total_fees = sum; |
| if (ping && result.value && typeof result.value === "string") { | ||
| res.status(200).send(json); | ||
| } else { | ||
| res.status(500); |
Copilot
AI
Aug 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The health check logic is incomplete. If Redis ping succeeds but the gRPC version check fails, the response status is never set, potentially causing the handler to hang. The else branch should explicitly handle this case.
This pull request introduces a Redis-based caching layer to the Tari Explorer backend, refactoring multiple routes to utilize Redis for improved performance and reliability. It adds a centralized
cacheServiceand standardized cache key management, and updates dependencies and configuration to support these features. Additionally, there are minor workflow and configuration improvements.Redis Caching Integration and Refactoring:
cacheServicesingleton (utils/cacheService.ts) providing get/set/delete and utility methods for Redis-backed caching, with robust error handling and logging. ([utils/cacheService.tsR1-R177](https://github.com/tari-project/tari-explorer/pull/130/files#diff-d8caa00321810af66fa2f55d1a9af44542d288156e72fe3ab45d01e543a0405eR1-R177))cacheKeys.tsto standardize cache key naming and organization, including keys for blocks, mempool, mining stats, tip, and network stats. ([utils/cacheKeys.tsR1-R36](https://github.com/tari-project/tari-explorer/pull/130/files#diff-8156283b6b8b9227b6b1286db6ac95b1cd9fab4314d7e9bbef1a5ae06a68b5e4R1-R36))routes/blocks.ts,routes/mempool.ts,routes/miners.ts) to usecacheServiceandCacheKeysfor data retrieval, falling back to gRPC if cache is unavailable. This reduces direct gRPC calls and centralizes cache logic. ([[1]](https://github.com/tari-project/tari-explorer/pull/130/files#diff-1a413a9edee0323a69d674367c7fe5ef3ecc46582c578cd769b95eb7530c63c8R27-L30),[[2]](https://github.com/tari-project/tari-explorer/pull/130/files#diff-1a413a9edee0323a69d674367c7fe5ef3ecc46582c578cd769b95eb7530c63c8L42-R43),[[3]](https://github.com/tari-project/tari-explorer/pull/130/files#diff-1a413a9edee0323a69d674367c7fe5ef3ecc46582c578cd769b95eb7530c63c8L280-R266),[[4]](https://github.com/tari-project/tari-explorer/pull/130/files#diff-1a413a9edee0323a69d674367c7fe5ef3ecc46582c578cd769b95eb7530c63c8L295-L301),[[5]](https://github.com/tari-project/tari-explorer/pull/130/files#diff-811f09b98bed0f3309f7697a6364522b7e2c89042f902d710c3f5c48b0a6025aL25-R27),[[6]](https://github.com/tari-project/tari-explorer/pull/130/files#diff-811f09b98bed0f3309f7697a6364522b7e2c89042f902d710c3f5c48b0a6025aL35-R46),[[7]](https://github.com/tari-project/tari-explorer/pull/130/files#diff-707097915f5b05c9300dea17fa4b4e9fdf1181cbf87bae716530711842bf9ae0L25-R40))routes/healthz.ts) to include Redis availability as part of the health status, and added logging. ([routes/healthz.tsR24-R47](https://github.com/tari-project/tari-explorer/pull/130/files#diff-549c7838519e16356be39b36c88cbd3b382c8fe24d539ddd4b4be6a41771c05dR24-R47))index.tsindex.tsR10-R25)Configuration and Dependency Updates:
ioredisand@types/ioredisfor Redis support, and@eslint/compatfor improved ESLint compatibility. (package.json[1] [2] [3].gitignorepatterns for file ignoring, improving linting accuracy. (eslint.config.js[1] [2]Workflow and Miscellaneous Improvements:
.github/workflows/build_docker.yml[1] [2]routes/index.ts[1] [2] [3]These changes collectively improve backend performance, reliability, and maintainability by leveraging Redis caching and centralizing cache logic.