Jedis Cluster setex() Method: Setting Keys with Expiration in Redis
JedisCluster.setex() is a method used to set the value of a key in a Redis Cluster with an expiration time.
Syntax:
jedisCluster.setex(key, seconds, value)
Parameters:
- 'key': The key to be set.
- 'seconds': The expiration time in seconds.
- 'value': The value to be set for the key.
Return Value:
The method returns 'OK' if the key was set successfully.
Example:
import redis.clients.jedis.*;
public class RedisClusterExample {
    public static void main(String[] args) {
        // Creating a Jedis Cluster object
        JedisCluster jedisCluster = new JedisCluster(new HostAndPort("localhost", 6379));
        // Setting a key-value pair with an expiration time of 60 seconds
        String key = "myKey";
        int expirationTimeInSeconds = 60;
        String value = "myValue";
        jedisCluster.setex(key, expirationTimeInSeconds, value);
        // Getting the value for the key
        String retrievedValue = jedisCluster.get(key);
        System.out.println("Retrieved value: " + retrievedValue);
        
        // Closing the Jedis Cluster connection
        jedisCluster.close();
    }
}
In the above example, we are setting a key 'myKey' with a value 'myValue' and an expiration time of 60 seconds using jedisCluster.setex method. We then retrieve the value using jedisCluster.get method and print it. Finally, we close the Jedis Cluster connection.
 
原文地址: https://www.cveoy.top/t/topic/b4up 著作权归作者所有。请勿转载和采集!