Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,16 @@ THROTTLE_TTL_MS=60000
# Maximum number of requests allowed per TTL window
# Default: 10 requests per minute
THROTTLE_LIMIT=10

# ============================================
# Redis Cache Configuration
# ============================================

# REDIS_URL (optional)
# Redis connection URL for caching layer
# Format: redis://[password@]host:port/db
# Example: redis://localhost:6379/0
# Example with password: redis://:password@localhost:6379/0
# Example with ElastiCache: redis://my-cluster.xxxxxx.use1.cache.amazonaws.com:6379/0
# Leave empty to disable caching (degrades gracefully)
REDIS_URL=
74 changes: 74 additions & 0 deletions Backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"cookie-parser": "^1.4.7",
"csrf": "^3.1.0",
"ethers": "^6.15.0",
"ioredis": "^5.11.1",
"pg": "^8.13.3",
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1",
Expand Down
2 changes: 2 additions & 0 deletions Backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { IpfsModule } from './ipfs/ipfs.module';
import { SorobanModule } from './soroban/soroban.module';
import { GistsModule } from './gists/gists.module';
import { HealthModule } from './health/health.module';
import { CacheModule } from './cache/cache.module';
import { AppController } from './app.controller';
import { AppService } from './app.service';

Expand All @@ -30,6 +31,7 @@ import { AppService } from './app.service';
SorobanModule,
GistsModule,
HealthModule,
CacheModule,
],
controllers: [AppController],
providers: [
Expand Down
10 changes: 10 additions & 0 deletions Backend/src/cache/cache.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { CacheService } from './cache.service';

@Module({
imports: [ConfigModule],
providers: [CacheService],
exports: [CacheService],
})
export class CacheModule {}
121 changes: 121 additions & 0 deletions Backend/src/cache/cache.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ConfigService } from '@nestjs/config';
import { CacheService } from './cache.service';

describe('CacheService', () => {
let service: CacheService;
let mockConfigService: jest.Mocked<ConfigService>;

beforeEach(async () => {
mockConfigService = {
get: jest.fn(),
} as unknown as jest.Mocked<ConfigService>;

const module: TestingModule = await Test.createTestingModule({
providers: [
CacheService,
{
provide: ConfigService,
useValue: mockConfigService,
},
],
}).compile();

service = module.get<CacheService>(CacheService);
});

afterEach(async () => {
if (service) {
await service.onModuleDestroy();
}
});

describe('onModuleInit', () => {
it('should not initialize Redis when REDIS_URL is not configured', async () => {
mockConfigService.get.mockReturnValue(undefined);
await service.onModuleInit();
// Service should gracefully degrade without Redis
});

it('should handle Redis connection errors gracefully when REDIS_URL is set', async () => {
mockConfigService.get.mockReturnValue('redis://localhost:6379/0');
// Since we don't have actual Redis, this will fail but should not throw
await service.onModuleInit();
// Service should gracefully degrade
});
});

describe('get', () => {
it('should return null when Redis is unavailable', async () => {
mockConfigService.get.mockReturnValue(undefined);
await service.onModuleInit();

const result = await service.get('test-key');
expect(result).toBeNull();
});

it('should track cache misses when Redis is unavailable', async () => {
mockConfigService.get.mockReturnValue(undefined);
await service.onModuleInit();

await service.get('test-key');
const metrics = service.getMetrics();

expect(metrics.hits).toBe(0);
expect(metrics.misses).toBe(1);
});
});

describe('set', () => {
it('should not attempt set when Redis is unavailable', async () => {
mockConfigService.get.mockReturnValue(undefined);
await service.onModuleInit();

await service.set('test-key', { data: 'test' }, 60);
// Should not throw
});
});

describe('del', () => {
it('should not attempt delete when Redis is unavailable', async () => {
mockConfigService.get.mockReturnValue(undefined);
await service.onModuleInit();

await service.del('test-key');
// Should not throw
});
});

describe('delPattern', () => {
it('should not attempt pattern delete when Redis is unavailable', async () => {
mockConfigService.get.mockReturnValue(undefined);
await service.onModuleInit();

await service.delPattern('gist:nearby:*');
// Should not throw
});
});

describe('getMetrics', () => {
it('should return 0 hit rate when no requests', () => {
const metrics = service.getMetrics();
expect(metrics.hitRate).toBe(0);
expect(metrics.hits).toBe(0);
expect(metrics.misses).toBe(0);
});
});

describe('resetMetrics', () => {
it('should reset hit and miss counters', async () => {
mockConfigService.get.mockReturnValue(undefined);
await service.onModuleInit();

await service.get('test-key');
service.resetMetrics();
const metrics = service.getMetrics();

expect(metrics.hits).toBe(0);
expect(metrics.misses).toBe(0);
});
});
});
Loading
Loading