🤑 wrap virtually everything that can store by key to act as cache with ttl/max-age, stale-while-validate, parallel fetch protection and type-safety support
MIT License
916
stars
26
forks
source link
redisCacheAdapter does not cache if ttl is set because of the #17
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:
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:
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 thegetFreshValue
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: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: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:I copied the
redisCacheAdapter
into my code and updated the setter to:And now it works for me. :)