-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(common): fix cache ttl not beeing respected #11131
Merged
+123
−3
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e151c47
fix(common): fix cache ttl not beeing respected
Flusinerd 63814af
Update packages/common/cache/interceptors/cache.interceptor.ts
Flusinerd e7b38a0
refactor(common): refactor cache-manager version detection
Flusinerd 7ef71f3
fix(common): fix cache-manager version detection running every request
Flusinerd ee11bea
Update packages/common/cache/interceptors/cache.interceptor.ts
kamilmysliwiec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will be called everytime we intercept something, which isn't needed as this won't change over time.
What about moving such logic to
constructor
instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, ye good catch. My bad :)