1 Star 4 Fork 1

scarf/springboot3-springsecurity6-jwt

 / 详情

redis 时区问题

Done
owner
Opened this issue  
2024-04-05 18:51

redisTemplate.expireAt 设置 Date 日期后与实际相差八小时。

Comments (10)

scarf created任务
scarf changed title
scarf changed title
scarf changed description
scarf added
 
question
label
scarf set assignee to scarf
scarf changed assignee from scarf to not set
scarf set assignee to scarf
scarf changed assignee from scarf to not set
Expand operation logs

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

// ...

Date originalDate = // 原始日期
ZonedDateTime originalZonedDateTime = originalDate.toInstant().atZone(ZoneId.systemDefault());
ZonedDateTime utcZonedDateTime = originalZonedDateTime.withZoneSameInstant(ZoneId.of("UTC"));
Date utcDate = Date.from(utcZonedDateTime.toInstant());

redisTemplate.expireAt(key, utcDate);

单元测试
缓存TTL
行不通,实际没有相差一天。

public void expireAt(String key, Date expire) {
    Instant instant = expire.toInstant();
    ZonedDateTime utcDateTime = instant.atZone(ZoneId.of("UTC"));
    redisTemplate.expireAt(key, new Date(utcDateTime.toInstant().toEpochMilli()));
}

bc60ae3d_9719256
cd2845ff_9719256
一样的兄弟

Date now = new Date();
now = DateUtils.addDay(now ,1);
redisTemplate.expireAt("test", now );

再或者 直接 计算一天后的毫秒数, 直接设置过期时间 1000 * 60 *60 *24

指定 ttl 确实可以,不过要是想要正确设置 Date 估计得看系统吧,之前在 window 上可行

redis里面的日期没有设置时区或者是你服务器没有设置时区? 你这个正好差八小时 不就是 北京时间或者上海时间?

看了一下,虚拟机同步了本机时间,导致utc时间有误。取消时间同步就好了。属于错在了意向不到的地方。

之前也以为是时区问题,后面发现redis不论机器时区是啥只看utc。

111

scarf changed issue state from 待办的 to 已完成
scarf changed issue state from 已完成 to 进行中
scarf changed issue state from 进行中 to 已完成

Sign in to comment

Status
Assignees
Milestones
Pull Requests
Successfully merging a pull request will close this issue.
Branches
Planed to start   -   Planed to end
-
Top level
Priority
参与者(4)
9997491 karasukaigan 1664448219 9719256 wjsr 1712210038 10456722 hestudio admin 1722736254 5091950 acaterpillar 1650879772
Java
1
https://gitee.com/wjsr/springboot3-springsecurity6-jwt.git
git@gitee.com:wjsr/springboot3-springsecurity6-jwt.git
wjsr
springboot3-springsecurity6-jwt
springboot3-springsecurity6-jwt

Search