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

redisCacheAdapter does not cache if ttl is set because of the #17

Closed
TapaiBalazs opened this issue Dec 7, 2022 · 1 comment · Fixed by #18
Closed

redisCacheAdapter does not cache if ttl is set because of the #17

TapaiBalazs opened this issue Dec 7, 2022 · 1 comment · Fixed by #18
Labels

Comments

@TapaiBalazs
Copy link
Contributor

TapaiBalazs commented Dec 7, 2022

I have tried out this library for caching API requests with Redis (v4+), using the redisCacheAdapter for adapter.
I followed the documentation on how to set up caching, but every request made through the getFreshValue method I passed in it.

After some debugging, I found that the cachified call suppresses an error when setting a value into redis, so I tried manually calling the adapter's set method:

await cachifiedCache.set('key', { value: 1, metadata: { ttl: 60000, createdTime: new Date().getTime() } })

That revealed the error, which was the following: ERR value is not an integer or out of range. Which was strange, since the ttl value I passed should not be problematic, so I checked out the adapter's source code and found how the expiration is set:

{
  EXAT: (ttl + createdTime) / 1000
}

Based on the above, I think redis needs an unix timestamp passed to the EXAT param, that is why the / 1000 is there, but if we come from a javascript time, that won't be an integer:

const time = new Date().getTime() // something like: 1670395771645

console.log((300_000 + time) / 1000) // will log 1670396071.645

I copied the redisCacheAdapter into my code and updated the setter to:

{
  EXAT: Math.round((ttl + createdTime) / 1000),
}

And now it works for me. :)

@github-actions
Copy link

🎉 This issue has been resolved in version 3.0.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant