importredis.clients.jedis.Jedis;importredis.clients.jedis.exceptions.JedisConnectionException;publicclassRedisLazyDeleteExample{
publicstaticvoidmain(String[] args){
// 连接到本地的Redis服务Jedis jedis =newJedis("localhost",6379);try{
// 设置键值对,并设置过期时间为10秒
jedis.set("key1","value1");
jedis.expire("key1",10);// 获取数据时检查数据是否过期,如果过期则删除String value = jedis.get("key1");if(value ==null){
System.out.println("Key does not exist.");}else{
System.out.println("Value of key1: "+ value);Long ttl = jedis.ttl("key1");if(ttl !=-2&& ttl !=-1){
System.out.println("Key is not expired.");}else{
jedis.del("key1");System.out.println("Key has expired and has been deleted.");}}// 关闭连接
jedis.close();}catch(JedisConnectionException e){
System.err.println("Could not connect to Redis. Please make sure that the Redis server is running.");}}}