Skip to content
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

feature(common) Add CacheTTL decorator. Cache decorators priority over global defaults. #2943

Merged
merged 5 commits into from
Oct 31, 2019

Conversation

joeyslack
Copy link
Contributor

@joeyslack joeyslack commented Sep 12, 2019

PR Type

The CacheTTL decorator has been added to controller methods for adding additional cache features. This allows the user to set the Cache TTL before it's set in it's cache store.

Additionally, @CacheKey() and @CacheTTL() decorator usage with @CacheInterceptor() take priority over global Cache configuration defaults. This allows for sensible Global Caching defaults, with controller specific cache overrides.

[ ] Bugfix
[✓] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Other... Please describe:

What is the current behavior?

Currently, when Global Cache is enabled, controller specific CacheKey settings are ignored. This forces the user to annotate all of their cached methods, or globally cache all, making customization tedious and repetitive, and potentially dangerous.

Furthermore, TTL override hasn't been possible before on a controller routing.

Issue Number: N/A

What is the new behavior?

The new behavior allows the user to override CacheTTL when globals are set, or not.

When @CacheKey() or @CacheTTL() is provided on a cash intercepted route (@UseInterceptor()), it will take priority over default cache setting.

Does this PR introduce a breaking change?

[ ] Yes
[✓] No

Other information

Open to making modifications as necessary. I built this to solve a problem in my own application, hopefully it helps others. I think this is a very simple cache improvement that should have no side-effects, performance implications or otherwise.

@coveralls
Copy link

coveralls commented Sep 12, 2019

Pull Request Test Coverage Report for Build 3276

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 95.281%

Totals Coverage Status
Change from base Build 1977: 0.0%
Covered Lines: 4119
Relevant Lines: 4323

💛 - Coveralls

@@ -51,10 +58,12 @@ export class CacheInterceptor implements NestInterceptor {
trackBy(context: ExecutionContext): string | undefined {
const httpAdapter = this.httpAdapterHost.httpAdapter;
const isHttpApp = httpAdapter && !!httpAdapter.getRequestMethod;

if (!isHttpApp) {
return this.reflector.get(CACHE_KEY_METADATA, context.getHandler());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines shouldn't be removed - httpAdapter will be undefined for both microservices and websockets.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I thought about that. Correct me if I'm misunderstanding, but since the check is happening for the reflector data first, the check for !isHttpApp won't ever be necessary, as the reflector check is happening no matter what (causing that check to be meaningless now). Happy to make changes, but not sure if it makes any difference given the logic change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but what if someone works on microservice/WS application and forgot to set CACHE_KEY_METADATA? The following line:

if (cacheMetadata)

will return false, so Nest will call this one:

httpAdapter.getRequestMethod()

whereas httpAdapter will be undefined in this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the response. I'll patch.

@ruscon
Copy link

ruscon commented Sep 26, 2019

Maybe it makes sense to see how this is done in other frameworks?
For example, here https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/cache.html#http-validation-strategies

@joeyslack
Copy link
Contributor Author

@ruscon Fair discussion, I'm intimately familiar with symfony from a previous life.

However, this PR is just to compliment an already existing cache system, to match the cache strategy already in place. The idea is just to make sure both parameters (key and ttl) are modifiable.

I think, if you wanted to make new cache decorators entirely, that's cool - would love to contribute in another PR, but this PR was intended to stay completely in line with what already exists, and just allow users to override this one particular setting.

@kamilmysliwiec
Copy link
Member

@joeyslack would you like to create a PR to the docs with this feature? I'm planning to merge this shortly :) https://github.com/nestjs/docs.nestjs.com/pulls

Co-Authored-By: Kamil Mysliwiec <mail@kamilmysliwiec.com>
@joeyslack
Copy link
Contributor Author

@kamilmysliwiec Docs updated at nestjs/docs.nestjs.com#718

@kamilmysliwiec
Copy link
Member

Thank you @joeyslack!

@kamilmysliwiec kamilmysliwiec added this to the 6.9.0 milestone Oct 3, 2019
@kamilmysliwiec kamilmysliwiec changed the base branch from master to 6.9.0 October 31, 2019 12:18
@kamilmysliwiec kamilmysliwiec merged commit ddcee87 into nestjs:6.9.0 Oct 31, 2019
@ruscon
Copy link

ruscon commented Oct 31, 2019

@kamilmysliwiec
I have a question... Why don't you use rebase instead of merge strategy?

@kamilmysliwiec
Copy link
Member

I use both depending on what I need to achieve.

@nestjs nestjs locked as resolved and limited conversation to collaborators Oct 31, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants