-
-
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
Conversation
Pull Request Test Coverage Report for Build ded6960b-f2c1-4f0d-920b-4552aa922554
💛 - Coveralls |
const cacheManager = loadPackage('cache-manager', 'CacheModule', () => | ||
require('cache-manager'), | ||
); | ||
const cacheManagerIsv5OrGreater = 'memoryStore' in cacheManager; |
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.
Only the name of this constant cacheManagerIsv5OrGreater
I would change, to make it a little more readable, otherwise it looks good. 👍
Just one question the interceptor continues to work with both cache-manager v4 and v5, right?
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.
It falls back to using the old signature if the version is below v5. So there should be no change to the code for that case.
cacheManagerIsv5OrGreater
is beeing used in the packages/common/cache/cache.providers.ts
as well to use milliseconds instead of seconds for the default TTL of five seconds. So I assumed the check is correct.
Any suggestions about naming this?
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.
Ok, for the name cacheManagerIsv5OrGreater
if it's already used internally already in common then it's better to use this, sure it's a bit strange name 😄 . Anyway the variable name from what I understand should mean: " Cache manager is version 5 or higher "
.
At the moment one name could be "cacheManagerVersions"
, it is an idea 😄, if you change it here you should also change it to packages/common/cache/cache.providers.ts
.
Howerer if I have other names I will write to you here.
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.
" Cache manager is version 5 or higher "
This is exactly what the variable is ment to be.
It's a boolean that is true if the package beeing loaded is v5 or greater
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.
Yes, I agree with that, no problem, but I think the name could be improved, maybe even something like "cacheManagerCheckVersions
", however i repeat no problem 👍
const cacheManager = loadPackage('cache-manager', 'CacheModule', () => | ||
require('cache-manager'), | ||
); | ||
const cacheManagerIsv5OrGreater = 'memoryStore' in cacheManager; |
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.
we need to do this check lazily otherwise we end up forcing everyone to have cache-manager
installed
What about having a private method at CacheInterceptor
just for this role? it would return a boolean
Or, better yet(?), we could have a internal utility function at cache.providers.ts
that will do that check. Then we can use the same function on createCacheManager
as well to avoid duplicating logic
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.
Or, better yet(?), we could have a internal utility function at cache.providers.ts that will do that check. Then we can use the same function on createCacheManager as well to avoid duplicating logic
That seems like the best solution.
The loadPackage
function is logging when it could not load the dependency in its catch.
Do we want to modify that function with a parameter to disable that log, or do we want to refactor that function so it uses a new function without the logging?
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.
we should keep the current behavior, which is just like how createCacheManager
works.
Co-authored-by: Micael Levi L. Cavalcante <[email protected]>
refactor cache-manager version detection in CacheInterceptor to be lazy
const cacheManager = loadPackage('cache-manager', 'CacheModule', () => | ||
require('cache-manager'), | ||
); | ||
const cacheManagerIsv5OrGreater = 'memoryStore' in cacheManager; |
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 :)
move cache-manager version detection to constructor
I'm still having this issue. In tableplus the TTL on all of my records is -1.. [email protected] Using the following in my controller @Get()
@UseInterceptors(CacheInterceptor)
@CacheTTL(2 * 60) |
@Swift188 If you use cache-manager v5, you possibly want |
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Currently the
@CacheTTL
decorator does not work. With cachemanager v5Issue Number: #11119
What is the new behavior?
@CacheTTL
Works again and the behavior is inline with the docs (expects milliseconds)The interceptor has been updated to also check, like the provider does, if version 5 or later is beeing used.
If so the inteceptor uses the new signature for the
set(key, value, ttl)
method. Version 4 used an options object as third paremeter. V5 uses a number directly.Does this PR introduce a breaking change?
Because it checks which version of cache manager is beeing used.
Other information