숫자형 데이터 증감

✒️ 2026-07-01 20:04 내용 수정


실습 참고 자료

package myproject.redis.lettuce.string;  
  
import io.lettuce.core.RedisClient;  
import io.lettuce.core.RedisURI;  
import io.lettuce.core.api.StatefulRedisConnection;  
import io.lettuce.core.api.sync.RedisCommands;  
import org.junit.jupiter.api.Test;  
  
import java.time.Duration;  
  
public class RedisLettuceString {  
    @Test  
    public void incrDecr() {  
        // host 주소  
        String host = "localhost";  
  
        // Redis 연결 주소 생성  
        RedisURI redisURI = RedisURI.builder()  
                .withHost(host)  
                .withPort(6379) // 포트 번호. Docker에서 설정한 값과 동일하게 설정  
                .withDatabase(0) // 0 - 15까지 존재  
                .build();  
  
        // Redis 클라이언트 생성  
        RedisClient redisClient = RedisClient.create(redisURI);  
  
        // Connection 연결  
        StatefulRedisConnection<String, String> connection = redisClient.connect();  
  
        // Redis 명령어  
        RedisCommands<String, String> redisCommands = connection.sync();  
          
        // Redis 연결 테스트를 위한 Key-value        
        String key = "lettuce:string";  
        String value = "hello";  
          
        // Redis에 Key-value 저장  
        redisCommands.set(key, value);  
          
        // 저장된 Key로 value 확인  
        String get = redisCommands.get(key);  
        System.out.println("get = " + get);  
        
		// DB 사용이 완료되면 connection을 끊고 Client도 종료한다.
        connection.close();  
		redisClient.shutdown();
    }  
}

incr 메서드

@Test  
public void incrDecr() {  
	// 생략
  
    // Redis 연결 테스트를 위한 Key-value    
    String key = "lettuce:string";  
    String value = "hello";  
  
    // incr, decr  
    Long incr = redisCommands.incr(key);  
    System.out.println("incr = " + incr);  
    
    // 생략
}

springboot_redis 6.png

springboot_redis 7.png

@Test  
public void incrDecr() {  
	// 생략
  
    // Redis 연결 테스트를 위한 Key-value    
    String key = "lettuce:string";  
    String value = "hello";  
    
	// key에 String value 저장  
	redisCommands.set(key, value);
  
    // incr, decr  
    Long incr = redisCommands.incr(key);  
    System.out.println("incr = " + incr);  
    
    // 생략
}

springboot_redis 8.png


decr 메서드

@Test  
public void incrDecr() {  
	// 생략
  
    // Redis 연결 테스트를 위한 Key-value    
    String key = "lettuce:string";  
    String value = "hello";  
    
	// key에 String value 저장  
	redisCommands.set(key, value);
  
    // incr
    Long incr = redisCommands.incr(key);  
    System.out.println("incr = " + incr);  
    
    // decr
    Long decr = redisCommands.decr(key);  
	System.out.println("decr = " + decr);
    
    // 생략
}

springboot_redis 9.png


incrby, decrby 메서드

@Test  
public void incrDecr() {  
	// 생략
  
    // Redis 연결 테스트를 위한 Key-value    
    String key = "lettuce:string";  
    String value = "hello";  
    
	// key에 String value 저장  
	redisCommands.set(key, value);
  
    // incr
    Long incr = redisCommands.incr(key);  
    System.out.println("incr = " + incr);  
    
    // decr
    Long decr = redisCommands.decr(key);  
	System.out.println("decr = " + decr);
    
	// incrby  
	Long incrby = redisCommands.incrby(key, 10);  
	System.out.println("incrby = " + incrby);  
	  
	// decrby  
	Long decrby = redisCommands.decrby(key, 20);  
	System.out.println("decrby = " + decrby);
	    
    // 생략
}

springboot_redis 10.png