Skip to content

Commit efa661e

Browse files
authored
Merge pull request #293 from JoeX17/cache-stellar-hash-verification-results-in-redis
feat: cache Stellar hash verification results in Redis
2 parents df8ea76 + 9c8cc30 commit efa661e

2 files changed

Lines changed: 26 additions & 5 deletions

File tree

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import { Module } from '@nestjs/common';
2-
import { ConfigModule } from '@nestjs/config';
3-
import { StellarService } from './stellar.service';
2+
import { ConfigModule, ConfigService } from '@nestjs/config';
3+
import { StellarService, STELLAR_REDIS } from './stellar.service';
4+
import Redis from 'ioredis';
45

56
@Module({
67
imports: [ConfigModule],
7-
providers: [StellarService],
8+
providers: [
9+
{
10+
provide: STELLAR_REDIS,
11+
useFactory: (config: ConfigService) =>
12+
new Redis(config.get<string>('REDIS_URL') || 'redis://localhost:6379'),
13+
inject: [ConfigService],
14+
},
15+
StellarService,
16+
],
817
exports: [StellarService],
918
})
1019
export class StellarModule {}

backend/src/stellar/stellar.service.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import { Injectable, InternalServerErrorException, Logger } from '@nestjs/common';
1+
import { Injectable, InternalServerErrorException, Logger, Inject } from '@nestjs/common';
22
import { ConfigService } from '@nestjs/config';
33
import { Keypair, Networks, Operation, Server, TransactionBuilder } from 'stellar-sdk';
4+
import type { Redis } from 'ioredis';
5+
6+
export const STELLAR_REDIS = 'STELLAR_REDIS';
47

58
@Injectable()
69
export class StellarService {
@@ -10,7 +13,10 @@ export class StellarService {
1013
private readonly networkPassphrase: string;
1114
private readonly accountId: string;
1215

13-
constructor(private readonly configService: ConfigService) {
16+
constructor(
17+
private readonly configService: ConfigService,
18+
@Inject(STELLAR_REDIS) private readonly redis: Redis,
19+
) {
1420
const secretKey = this.configService.get<string>('STELLAR_SECRET_KEY');
1521
const horizonUrl = this.configService.get<string>('STELLAR_HORIZON_URL') || 'https://horizon-testnet.stellar.org';
1622
this.networkPassphrase =
@@ -65,12 +71,18 @@ export class StellarService {
6571
throw new InternalServerErrorException('Hash is required to verify a document');
6672
}
6773

74+
const cacheKey = `stellar:verify:${hash}`;
75+
const cached = await this.redis.get(cacheKey);
76+
if (cached !== null) return cached === 'true';
77+
6878
try {
6979
const key = this.buildDataKey(hash);
7080
await this.server.accountData(this.accountId, key);
81+
await this.redis.set(cacheKey, 'true');
7182
return true;
7283
} catch (error) {
7384
if (error?.response?.status === 404) {
85+
await this.redis.set(cacheKey, 'false', 'EX', 60);
7486
return false;
7587
}
7688
this.logger.error('Failed to verify document hash', error);

0 commit comments

Comments
 (0)