If you would have worked with the Redis command, you must be familiar with the below SET options that you can use to set expiration or TTL for a key.
Option | : | What it does? |
---|---|---|
EX | : | Sets expiration time in seconds. |
EXAT | : | Sets the Unix time at which the key will expire in seconds. |
PX | : | Sets expiration time in milliseconds. |
PXAT | : | Sets the Unix time at which the key will expire in milliseconds. |
KEEPTTL | : | Retain the TTL value associated with the key. |
Let us see examples of each of the with Spring Boot.
EX: Setting expiration time in seconds
@Autowired
private RedisTemplate redisTemplate;
private long expirationTimeInSeconds = 1;
public void setKeyWithEx() throws InterruptedException {
redisTemplate.opsForValue().set("city", "New York", Duration.ofSeconds(expirationTimeInSeconds));
Thread.sleep(3000);
System.out.println(redisTemplate.opsForValue().get("message"));
}
As we have set the EX duration of 1 second and have added a thread sleep of 3 seconds we get a null response as the key got deleted before retrieval.
EXAT: Setting the Unix time at which the key will expire in seconds.
public void setKeyWithEXAT() {
Instant instant = Instant.ofEpochSecond(expirationTimeInSeconds);
redisTemplate.opsForValue().set("city", "New York", Duration.between(Instant.now(),instant));
}
PX: Setting expiration time in milliseconds
@Autowired
private RedisTemplate redisTemplate;
private long expirationTimeInMilliSeconds = 1;
public void setKeyWithPX() {
redisTemplate.opsForValue().set("city", "New York", Duration.ofMillis(expirationTimeInMilliSeconds));
}
PXAT: Setting the Unix time at which the key will expire in milliseconds.
public void setKeyWithPXAT() {
Instant instant = Instant.ofEpochMilli(expirationTimeInMilliSeconds);
redisTemplate.opsForValue().set("city", "New York", Duration.between(Instant.now(),instant));
}
PXAT: we want to preserve the existing time-to-live (TTL) value of a key.
@Autowired
private StringRedisTemplate redisTemplate;
public void setKeepTtlValue() {
redisTemplate.execute(connection -> {
byte[] keyBytes = redisTemplate.getKeySerializer().serialize("city");
byte[] valueBytes = redisTemplate.getValueSerializer().serialize("Chicago"); //setting a new value
Boolean result = connection.set(keyBytes, valueBytes, Expiration.KEEP_TTL, RedisStringCommands.SetOption.UPSERT);
System.out.println(result);
});
}
You should get a result as a boolean true.
Related Tutorials
- String Boot + Redis - SET and GET String Commands Examples
- Spring Boot + Redis Cloud Configuration and Setup
Have Questions? Post them here!
- Create a Zip file using Java Code programmatically
- Eclipse : A java Runtime Environment (JRE) or Java Development kit (JDK) must be available
- How to Sort a LinkedList in Java
- Loading class com.mysql.jdbc.Driver. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver
- How to declare and initialize Array in Java Programming
- [Fix] java: integer number too large compilation error
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Reading .xls and .xlsx Excel file using Apache POI Java Library
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- How to get Client IP address using Java Code Example
- Truncate table using Java JDBC Example
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj]
- How to get file path in Idea IntelliJ IDE
- Java Generics explained with simple definition and examples
- Java SE 8 Update 301 available with various bug fixes and security improvements
- Java: Collect Stream as ArrayList or LinkedList
- Java JDBC Connection with PostgreSQL Driver Example
- How to check if Java main thread is alive
- How to fix Java nio NoSuchFileException wile reading a file
- Java 8+ get Day of the Week Examples with LocalDateTime, DateTime, ZonalDateTime and Instant classes
- Ways to Convert Integer or int to Long in Java
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Fix] Spring Boot: mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
- Java: The value of the local variable string is not used
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement
- Can Microsoft SharePoint Lists be synced and accessed offline without internet? - SharePoint
- How to Toggle Dark Mode in Microsoft 365 Word App on Mac - Microsoft
- How to turn off VoiceOver on Macbook using Keyboard Shortcut - MacOS
- How to insert image into Google Sheets cell - Google
- PHP header location function not called - PHP
- Command to know the installed Debian version? - Linux
- Android Alert Dialog with Checkboxes example - Android
- How to lock Notepad++ tabs? - NotepadPlusPlus