1- import { Injectable , InternalServerErrorException , Logger } from '@nestjs/common' ;
1+ import { Injectable , InternalServerErrorException , Logger , Inject } from '@nestjs/common' ;
22import { ConfigService } from '@nestjs/config' ;
33import { 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 ( )
69export 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