-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11131 from Flusinerd/cache-ttl-bug
fix(common): fix cache ttl not beeing respected
- Loading branch information
Showing
5 changed files
with
123 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { INestApplication } from '@nestjs/common'; | ||
import { Test } from '@nestjs/testing'; | ||
import * as request from 'supertest'; | ||
import { CustomTtlModule } from '../src/custom-ttl/custom-ttl.module'; | ||
|
||
describe('Caching Custom TTL', () => { | ||
let server; | ||
let app: INestApplication; | ||
|
||
beforeEach(async () => { | ||
const module = await Test.createTestingModule({ | ||
imports: [CustomTtlModule], | ||
}).compile(); | ||
|
||
app = module.createNestApplication(); | ||
server = app.getHttpServer(); | ||
await app.init(); | ||
}); | ||
|
||
it('should return a differnt value after the TTL is elapsed', async () => { | ||
await request(server).get('/').expect(200, '0'); | ||
await new Promise(resolve => setTimeout(resolve, 500)); | ||
await request(server).get('/').expect(200, '1'); | ||
}); | ||
|
||
it('should return the cached value within the TTL', async () => { | ||
await request(server).get('/').expect(200, '0'); | ||
await new Promise(resolve => setTimeout(resolve, 200)); | ||
await request(server).get('/').expect(200, '0'); | ||
}); | ||
|
||
afterEach(async () => { | ||
await app.close(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { | ||
CacheInterceptor, | ||
CacheTTL, | ||
Controller, | ||
Get, | ||
UseInterceptors, | ||
} from '@nestjs/common'; | ||
|
||
@Controller() | ||
export class CustomTtlController { | ||
counter = 0; | ||
constructor() {} | ||
|
||
@Get() | ||
@CacheTTL(500) | ||
@UseInterceptors(CacheInterceptor) | ||
getNumber() { | ||
return this.counter++; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { CacheModule, Module } from '@nestjs/common'; | ||
import { CustomTtlController } from './custom-ttl.controller'; | ||
|
||
@Module({ | ||
imports: [CacheModule.register()], | ||
controllers: [CustomTtlController], | ||
}) | ||
export class CustomTtlModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"declaration": false, | ||
"noImplicitAny": false, | ||
"removeComments": true, | ||
"noLib": false, | ||
"emitDecoratorMetadata": true, | ||
"experimentalDecorators": true, | ||
"target": "es6", | ||
"sourceMap": true, | ||
"allowJs": true, | ||
"outDir": "./dist", | ||
"paths": { | ||
"@nestjs/common": ["../../packages/common"], | ||
"@nestjs/common/*": ["../../packages/common/*"], | ||
"@nestjs/core": ["../../packages/core"], | ||
"@nestjs/core/*": ["../../packages/core/*"], | ||
"@nestjs/microservices": ["../../packages/microservices"], | ||
"@nestjs/microservices/*": ["../../packages/microservices/*"], | ||
"@nestjs/websockets": ["../../packages/websockets"], | ||
"@nestjs/websockets/*": ["../../packages/websockets/*"], | ||
"@nestjs/testing": ["../../packages/testing"], | ||
"@nestjs/testing/*": ["../../packages/testing/*"], | ||
"@nestjs/platform-express": ["../../packages/platform-express"], | ||
"@nestjs/platform-express/*": ["../../packages/platform-express/*"], | ||
"@nestjs/platform-socket.io": ["../../packages/platform-socket.io"], | ||
"@nestjs/platform-socket.io/*": ["../../packages/platform-socket.io/*"], | ||
"@nestjs/platform-ws": ["../../packages/platform-ws"], | ||
"@nestjs/platform-ws/*": ["../../packages/platform-ws/*"] | ||
} | ||
}, | ||
"include": [ | ||
"src/**/*", | ||
"e2e/**/*" | ||
], | ||
"exclude": [ | ||
"node_modules", | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters