How to set the redis validity period
This article mainly explains "how to set the period of validity of redis". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to set the validity period of redis".
1 about redisKey construction
Private String key (RedisShip I) {
/ / return String.format ("% s_%d_%d_%d", KEY_PREFIX, getNow (), i.getPosTime (), i.getGrid ())
Return String.format ("% s:%d:%d:%d", KEY_PREFIX, getNow (), i.getPosTime (), i.getGrid ());}
2 about getting the current time 0: 00
Public Integer getNow () {/ / Calendar calendar = Calendar.getInstance (); / / calendar.setTime (new Date ()); / / calendar.set (calendar.get (Calendar.YEAR), / / calendar.get (Calendar.MONTH), calendar.get (Calendar.DATE), / / 0,0,0); return Math.toIntExact (Instant.now (). GetEpochSecond () / 86400 * 86400);}
3 about setting the period of validity of redis
Private static final Duration DURATION = Duration.ofDays (2)
Connection.expire (key, DURATION.getSeconds ())
Thank you for reading, the above is the content of "how to set the period of validity of redis". After the study of this article, I believe you have a deeper understanding of how to set the validity period of redis, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!