|
| 1 | +import { Injectable, Logger } from '@nestjs/common'; |
| 2 | +import { RedisService } from '../redis/redis.service'; |
| 3 | +import { ConfigService } from '@nestjs/config'; |
| 4 | + |
| 5 | +@Injectable() |
| 6 | +export class GovernanceCache { |
| 7 | + private readonly logger = new Logger(GovernanceCache.name); |
| 8 | + private readonly ttl: number; |
| 9 | + |
| 10 | + constructor( |
| 11 | + private readonly redisService: RedisService, |
| 12 | + private readonly configService: ConfigService, |
| 13 | + ) { |
| 14 | + this.ttl = this.configService.get<number>('CACHE_GOVERNANCE_TTL', 3600); |
| 15 | + } |
| 16 | + |
| 17 | + private getProposalKey(id: string): string { |
| 18 | + return `governance:proposal:${id}`; |
| 19 | + } |
| 20 | + |
| 21 | + private getActiveProposalsKey(): string { |
| 22 | + return 'governance:proposals:active'; |
| 23 | + } |
| 24 | + |
| 25 | + private getAllProposalsKey(): string { |
| 26 | + return 'governance:proposals:all'; |
| 27 | + } |
| 28 | + |
| 29 | + private getStatsKey(): string { |
| 30 | + return 'governance:stats'; |
| 31 | + } |
| 32 | + |
| 33 | + private getVotesKey(proposalId: string): string { |
| 34 | + return `governance:votes:${proposalId}`; |
| 35 | + } |
| 36 | + |
| 37 | + async getProposal(id: string): Promise<any | null> { |
| 38 | + const data = await this.redisService.get(this.getProposalKey(id)); |
| 39 | + if (data) { |
| 40 | + this.logger.debug(`Cache hit for governance proposal:${id}`); |
| 41 | + try { |
| 42 | + return JSON.parse(data); |
| 43 | + } catch { |
| 44 | + return null; |
| 45 | + } |
| 46 | + } |
| 47 | + this.logger.debug(`Cache miss for governance proposal:${id}`); |
| 48 | + return null; |
| 49 | + } |
| 50 | + |
| 51 | + async setProposal(id: string, proposal: any): Promise<void> { |
| 52 | + await this.redisService.set( |
| 53 | + this.getProposalKey(id), |
| 54 | + JSON.stringify(proposal), |
| 55 | + this.ttl, |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + async getActiveProposals(): Promise<any[] | null> { |
| 60 | + const data = await this.redisService.get(this.getActiveProposalsKey()); |
| 61 | + if (data) { |
| 62 | + this.logger.debug('Cache hit for governance:proposals:active'); |
| 63 | + try { |
| 64 | + return JSON.parse(data); |
| 65 | + } catch { |
| 66 | + return null; |
| 67 | + } |
| 68 | + } |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + async setActiveProposals(proposals: any[]): Promise<void> { |
| 73 | + await this.redisService.set( |
| 74 | + this.getActiveProposalsKey(), |
| 75 | + JSON.stringify(proposals), |
| 76 | + this.ttl, |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + async getStats(): Promise<any | null> { |
| 81 | + const data = await this.redisService.get(this.getStatsKey()); |
| 82 | + if (data) { |
| 83 | + this.logger.debug('Cache hit for governance:stats'); |
| 84 | + try { |
| 85 | + return JSON.parse(data); |
| 86 | + } catch { |
| 87 | + return null; |
| 88 | + } |
| 89 | + } |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + async setStats(stats: any): Promise<void> { |
| 94 | + await this.redisService.set( |
| 95 | + this.getStatsKey(), |
| 96 | + JSON.stringify(stats), |
| 97 | + this.ttl, |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + async getVotes(proposalId: string): Promise<any[] | null> { |
| 102 | + const data = await this.redisService.get(this.getVotesKey(proposalId)); |
| 103 | + if (data) { |
| 104 | + this.logger.debug(`Cache hit for governance:votes:${proposalId}`); |
| 105 | + try { |
| 106 | + return JSON.parse(data); |
| 107 | + } catch { |
| 108 | + return null; |
| 109 | + } |
| 110 | + } |
| 111 | + return null; |
| 112 | + } |
| 113 | + |
| 114 | + async setVotes(proposalId: string, votes: any[]): Promise<void> { |
| 115 | + await this.redisService.set( |
| 116 | + this.getVotesKey(proposalId), |
| 117 | + JSON.stringify(votes), |
| 118 | + this.ttl, |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + async invalidateProposal(id: string): Promise<void> { |
| 123 | + const promises = [ |
| 124 | + this.redisService.del(this.getProposalKey(id)), |
| 125 | + this.redisService.del(this.getActiveProposalsKey()), |
| 126 | + this.redisService.del(this.getAllProposalsKey()), |
| 127 | + this.redisService.del(this.getStatsKey()), |
| 128 | + this.redisService.del(this.getVotesKey(id)), |
| 129 | + ]; |
| 130 | + await Promise.all(promises); |
| 131 | + this.logger.debug(`Invalidated cache for governance proposal:${id}`); |
| 132 | + } |
| 133 | + |
| 134 | + async invalidateAll(): Promise<void> { |
| 135 | + const promises = [ |
| 136 | + this.redisService.del(this.getActiveProposalsKey()), |
| 137 | + this.redisService.del(this.getAllProposalsKey()), |
| 138 | + this.redisService.del(this.getStatsKey()), |
| 139 | + ]; |
| 140 | + await Promise.all(promises); |
| 141 | + this.logger.debug('Invalidated all governance caches'); |
| 142 | + } |
| 143 | +} |
0 commit comments