PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, September 5, 2022

[FIXED] How to make redis expire at an exact time in java?

 September 05, 2022     java, redis     No comments   

Issue

example: I want make redis string "aKey":"aValue" expired at a future time(2018.08.17 00:00, now is 2018.08.16 12:00), this is my solution:

long expireTime = Date.from(LocalDateTime.now().with(LocalTime.MAX).atZone(ZoneId.systemDefault()).toInstant()).getTime() - Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()).getTime();
redisTemplate.opsForValue().set("aKey","aValue",expireTime, TimeUnit.SECONDS);

I want a perfect solution.


Solution

Thanks for your help, I got it. Redis has an "EXPIREAT" command, which can be used like this in Java:

redisTemplate.opsForValue().set("aKey","aValue");
redisTemplate.expireAt("aKey",Date.from(LocalDateTime.now().with(LocalTime.MAX).atZone(ZoneId.systemDefault()).toInstant()));


Answered By - YuJia
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing