micronaut-projects / micronaut-redis

Integration between Micronaut and Redis
Apache License 2.0
31 stars 35 forks source link

Redis Lettuce Expiration Set Incorrectly #474

Open KeithWong98 opened 9 months ago

KeithWong98 commented 9 months ago

Expected Behavior

If I set the expire-after-write and expire-after-access

redis:
  caches:
    accounts:
      expire-after-access: 24h
      expire-after-write: 24h

I expect the Redis TTL to be set at 24 hours, or 86400 seconds.

Actual Behaviour

The cache TTL value is being set to 86400000 seconds, which is 1000 times the intended amount.

I discovered this after noticing via Redis Insight that the TTL was showing as over 2 years.

Screenshot 2024-01-10 at 10 19 28 AM

I then enabled trace logging on my project, and realized that the value coming back was 86400000 instead of the intended 86400 seconds. Here was the logging output:

$6
EXPIRE
$8
86400000

It seems to be a problem with converting the value of expireAfterAccess to milliseconds via the Duration::toMillis mapping. An example is found here in AbstractRedisCache.java:

this.expireAfterAccess = redisCacheConfiguration
.getExpireAfterAccess()
.map(Duration::toMillis)
.orElse(defaultRedisCacheConfiguration.getExpireAfterAccess().map(Duration::toMillis).orElse(null));

It seems the value of expireAfterAccess is still being treated as seconds instead of the expected milliseconds.

Steps To Reproduce

  1. Set the expire-after-access and expire-after-write within application.yml to 24h.
  2. Run the project.
  3. Check your cache on a Redis client (such as Redis Insight)
  4. Inspect the Time To Live value.

Environment Information

JDK Version 17

Example Application

No response

Version

4.2.0

Mike-Svendsen-IL commented 2 months ago

Since I was in the code working on some other PRs, I took a quick look at this issue as well, but I could not reproduce.

The expiration code appears to be working as expected - verified via existing unit tests and I added an additional one on my fork trying to reproduce the behavior, but couldn't. I reviewed the relevant code and it appears to be using the psetex redis command everywhere. So it is expected to be setting the ttl with millisecond precision.

@KeithWong98 any additional insights? If you use the pttl or ttl commands via the redis cli what values do you get back?

sdelamo commented 1 month ago

There is a bug indeed. @ChaimaaeROUAI is going to fix it in via https://github.com/micronaut-projects/micronaut-redis/pull/558