Computer >> 컴퓨터 >  >> 프로그램 작성 >> Redis

Redis Jedis – jedis 라이브러리를 사용하여 목록 값에 대한 CRUD 작업을 수행하는 방법

이 튜토리얼에서는 Jedis 라이브러리를 사용하여 목록 값에 대해 CRUD 연산을 수행하는 방법에 대해 알아봅니다.

제디스 도서관

Jedis는 redis 데이터 저장소용 Java 클라이언트 라이브러리입니다. 작고 사용하기 매우 쉬우며 redis 2.8.x, 3.x.x 이상 데이터 저장소와 완벽하게 호환됩니다. jedis 라이브러리에 대한 자세한 정보는 여기에서 확인할 수 있습니다.

목록 값

목록은 삽입 순서에 따라 정렬된 문자열 시퀀스입니다. Redis에서는 list를 값으로 저장할 수 있으며 redis 데이터베이스에 저장된 list 값을 저장, 관리 및 검색하기 위해 다양한 redis 명령을 사용할 수 있습니다. 목록 명령에 대한 자세한 정보는 여기에서 찾을 수 있습니다.

프로젝트 설정

좋아하는 IDE에서 간단한 maven 프로젝트를 만들고 pom.xml에 아래에 언급된 종속성을 추가합니다. 파일.

<dependency>
  <groupId>redis.clients</groupId>
  <artifactId>jedis</artifactId>
  <version>3.0.1</version>
</dependency>

jedis 라이브러리의 최신 버전은 이 페이지에서 확인하세요.

Redis 설치

최신 버전의 Redis를 설치해야 합니다. redis 설치에 대한 자세한 내용은 이 페이지를 확인하세요.

제다이 커넥션

Jedis의 개체 만들기 ( redis.clients.jedis.Jedis ) 자바 코드를 redis에 연결하기 위한 클래스

Jedis jedis = new Jedis();

로컬 컴퓨터와 기본 포트(6379)에서 하나의 redis 서비스를 시작한 경우 기본 생성자가 제대로 작동합니다. 그렇지 않으면 올바른 호스트 URL과 포트 번호를 전달해야 합니다. 생성자에 대한 인수로.

만들기 및 추가

Jedis 라이브러리는 목록 값에 요소를 만들고 삽입하는 여러 방법을 제공합니다. 몇 가지 중요한 방법은 다음과 같습니다:-

  1. lpush :-  목록 값의 맨 앞에 하나 이상의 요소를 삽입합니다. 목록 값이 존재하지 않으면 삽입 작업을 수행하기 전에 먼저 빈 목록 값을 보유하는 키를 만듭니다.
    /* Creating a new list and inserting string values a, b, c at head */
    jedis.lpush("list-1", "a", "b", "c");
    		
    /* Creating a new list and inserting byte array of string value 1, 2 at head */
    jedis.lpush("list-2".getBytes(),new BigInteger("1").toByteArray(),new BigInteger("2").toByteArray());
  2. rpush :- 목록 값의 꼬리 부분에 하나 이상의 요소를 삽입합니다. 목록 값이 존재하지 않으면 삽입 작업을 수행하기 전에 먼저 빈 목록 값을 보유하는 키를 만듭니다.
    /* Creating a new list and inserting string values a, b, c at tail */
    jedis.rpush("list-3", "a", "b", "c");
    		
    /* Creating a new list and inserting byte array of string value 1, 2 at tail */
    jedis.rpush("list-4".getBytes(),new BigIntege r("1").toByteArray(),new BigInteger("2").toByteArray());
  3. lpushx :- 리스트 값이 이미 존재하는 경우에만 리스트 값의 선두에 하나 이상의 요소를 삽입합니다.
    /* Inserting string values d, e at head of the list <list-1> */
    jedis.lpushx("list-1", "d", "e");
    		
    /* Inserting string values 3, 4 at head of the list <list-2> */
    jedis.lpushx("list-2".getBytes(),new BigInteger("3").toByteArray(),new BigInteger("4").toByteArray());
  4. rpushx :- 목록 값이 이미 존재하는 경우에만 목록 값의 꼬리에 하나 이상의 요소를 삽입합니다.
    /* Inserting string values d, e at tail of the list <list-3> */
    jedis.rpush("list-3", "d", "e");
    		
    /* Inserting string values 3, 4 at tail of the list <list-4> */
    jedis.rpush("list-4".getBytes(),new BigInteger("3").toByteArray(),new BigInteger("4").toByteArray());

제거 및 반환

목록 값에 대해 팝 연산과 같은 스택을 수행하는 방법에는 두 가지가 있습니다. 다음과 같습니다 :-

  1. lpop :- 목록 값의 첫 번째 요소를 제거하고 반환합니다.
    /* Removes and return single element from the head of <list-1> */
    jedis.lpop("list-1");
    		
    /* Removes and return single element from the head of <list-2> */
    jedis.lpop("list-2".getBytes());
  2. rpop :- 목록 값의 마지막 요소를 제거하고 반환합니다.
    /* Removes and return single element from the tail of <list-3> */
    jedis.rpop("list-3");
    		
    /* Removes and return single element from the tail of <list-4> */
    jedis.rpop("list-4".getBytes());

길이 

엘렌 메소드는 키에 저장된 목록 값의 길이를 얻는 데 사용됩니다. 코드 예:- 

/* Returns size of <list-1> */
jedis.llen("list-1");
		
/* Returns size of <list-2> */
jedis.llen("list-2".getBytes());

삭제 

메소드는 목록 값에서 요소의 모든 발생을 삭제하는 데 사용됩니다.

/* Remove one occurrence of element a from <list-1> */
jedis.lrem("list-1", 1, "a");
		
/* Remove two occurrence of element 1 from <list-2> */
jedis.lrem("list-2".getBytes(), 2, "1".getBytes());

색인별 가져오기 

인덱스 메서드는 인덱스 인수로 요소를 가져오는 데 사용됩니다.

/* Get an element at index 3 from <list-1> */
jedis.lindex("list-1", 3);
		
/* Get an element at index 2 from <list-2> */
jedis.lindex("list-2".getBytes(), 2);

색인별 업데이트 

이제 메서드는 인덱스 인수로 요소를 업데이트하는 데 사용됩니다.

/* Set an element g at index 3 from <list-1> */
jedis.lset("list-1", 3, "g");
		
/* Set an element 6 at index 2 from <list-2> */
jedis.lset("list-2".getBytes(), 2, "6".getBytes());

여러 요소 가져오기

범위 메서드는 offset 인수로 정의된 목록 값에서 하나 이상의 요소를 가져오는 데 사용됩니다.

/* Returns elements from index 1 to 5 from <list-1> */
jedis.lrange("list-1", 1, 5);
		
/* Returns all elements from <list-2> */
jedis.lrange("list-2".getBytes(), 0, -1);

참조:-

  1. 명령 문서 나열

Jedis 라이브러리를 사용하여 목록 값에 대해 CRUD 작업을 수행하는 방법은 여기까지입니다. 마음에 드셨다면 댓글 섹션에 의견을 공유하고 다른 사람들과도 공유해 주세요.