Skip to content
Closed
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
7 changes: 4 additions & 3 deletions libs/nestjs-cacheable/src/cacheable.interceptor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('CacheableInterceptor', () => {

const mockCacheService = {
get: jest.fn(),
set: jest.fn(),
set: jest.fn().mockResolvedValue(true),
}

const mockReflector = {
Expand All @@ -22,6 +22,7 @@ describe('CacheableInterceptor', () => {
const mockExecutionContext = {
switchToHttp: () => ({
getRequest: () => ({
method: 'GET',
url: '/test',
}),
}),
Expand Down Expand Up @@ -74,7 +75,7 @@ describe('CacheableInterceptor', () => {
const result$ = await interceptor.intercept(mockExecutionContext, mockCallHandler)
const value = await firstValueFrom(result$)
expect(value).toBe('new_value')
expect(mockCacheService.set).toHaveBeenCalledWith('/test', 'new_value', undefined)
expect(mockCacheService.set).toHaveBeenCalledWith('GET:/test', 'new_value', undefined)
})

it('should use TTL from decorator if present', async () => {
Expand All @@ -83,6 +84,6 @@ describe('CacheableInterceptor', () => {
mockReflector.get.mockReturnValue(5000)
const result$ = await interceptor.intercept(mockExecutionContext, mockCallHandler)
await firstValueFrom(result$)
expect(mockCacheService.set).toHaveBeenCalledWith('/test', 'new_value', 5000)
expect(mockCacheService.set).toHaveBeenCalledWith('GET:/test', 'new_value', 5000)
})
})
6 changes: 3 additions & 3 deletions libs/nestjs-cacheable/src/cacheable.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ export class CacheableInterceptor implements NestInterceptor {
const key = this.getCacheKey(context)
const cachedValue = await this.cacheService.get(key)

if (cachedValue) {
if (cachedValue !== undefined) {
return of(cachedValue)
}

const ttl = this.reflector.get<number>(CACHE_TTL_KEY, context.getHandler())

const value = await firstValueFrom(next.handle())
await this.cacheService.set(key, value, ttl)
this.cacheService.set(key, value, ttl).catch(() => {})
return of(value)
}

private getCacheKey(context: ExecutionContext): string {
// A simple key generation strategy. This can be improved later.
const httpContext = context.switchToHttp()
const request = httpContext.getRequest()
return request.url
return `${request.method}:${request.url}`
}
}
10 changes: 6 additions & 4 deletions libs/nestjs-cacheable/src/nestjs-cacheable.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@ export class NestjsCacheableService implements OnModuleDestroy {
}

async disconnect() {
if (this.cache.secondary && typeof this.cache.secondary.disconnect === 'function') {
await this.cache.secondary.disconnect()
}
await this.cache.disconnect()
}

async get<T>(key: string): Promise<T | undefined> {
return this.cache.get(key)
}

async set(key: string, value: any, ttl?: number): Promise<boolean> {
this.logger.log(`Setting cache for key: ${key}, value: ${JSON.stringify(value)}, ttl: ${ttl}`)
try {
this.logger.log(`Setting cache for key: ${key}, value: ${JSON.stringify(value)}, ttl: ${ttl}`)
} catch {
this.logger.log(`Setting cache for key: ${key}, value: [unserializable], ttl: ${ttl}`)
}
if (ttl !== undefined) {
await this.cache.set(key, value, ttl)
} else {
Expand Down