@SpringBootTest class SecurityApplicationTests { @Autowired StringRedisTemplate template; //添加这句代码,自动装载,即可解决文章三处代码报错 @Test void contextLoads() { String compact = Jwts.builder() .signWith(Jwts.SIG.HS512.key().build()) .subject("严欣铷") .expiration(new Date(System.currentTimeMillis() + 6000)) .compact(); long date = 60000; //6秒过期,6秒后自动删除 if (set(compact,date)){ System.out.println("导入成功"); } else { System.out.println("导入失败"); } } Boolean set(String token, long date){ try{ template.opsForValue().set("yanxinru",token,date,TimeUnit.MICROSECONDS); return true; }catch (Exception e){ return false; } } }
猜猜看,问题出在了哪里!
.
.
.
.
.
.
公布答案
TimeUnit.MICROSECONDS —— 微秒
60000w微秒 = 0.6毫秒
来不存储,就已经删除了
MILLISECONDS MICROSECONDS
这两个单词很像,稍微不注意,你就会认为代码有问题