Redis Client - Jdies Quick Start

The address of the original text is updated, and the reading effect is better!

Redis Client - Jdies Quick Start | CoderMast Programming Mast Redis Client - Jdies Quick Start Introduction Jedis is a Java client for Redis designed for performance and ease of use. Jedis is a Java client for Redis, designed for performance and ease of use. Official address Official address of Jedis: https://github.com/redis/jedis https://www.codermast.com/database/redis/jedis-quick-start.html

Introduction

Jedis is a Java client for Redis designed for performance and ease of use.

Jedis is a Java client for Redis, designed for performance and ease of use.

#official address

Official address of Jedis: https://github.com/redis/jedisopen in new window

#quickstart _

  1. Create a new Maven project and introduce the following dependencies

<!--引入Jedis依赖-->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>4.2.0</version>
</dependency>

<!--引入单元测试依赖-->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.8.2</version>
    <scope>test</scope>
</dependency>
  1. Write a test class and establish a connection with Redis

private Jedis jedis;

@BeforeEach //被该注解修饰的方法每次执行其他方法前自动执行
void setUp(){
    // 1. 获取连接
    jedis = new Jedis("192.168.230.88",6379);
    // 2. 设置密码
    jedis.auth("codermast");
    // 3. 选择库(默认是下标为0的库)
    jedis.select(0);
}
  1. Write a method for manipulating data (here we take the operation of String type as an example)

@Test
public void testString(){
    // 1.往redis中存放一条String类型的数据并获取返回结果
    String result = jedis.set("url", "https://www.codermast.com");
    System.out.println("result = " + result);

    // 2.从redis中获取一条数据
    String url = jedis.get("url");
    System.out.println("url = " + url);
}
  1. Finally, write a method to release resources

    @AfterEach //被该注解修饰的方法会在每次执行其他方法后执行
    void tearDown(){
        // 1.如果jedis被使用过,则释放资源
        if (jedis != null){
            jedis.close();
        }
    }
  1. Test the result after executing the testString() method.

result = OK
url = https://www.codermast.com

Guess you like

Origin blog.csdn.net/qq_33685334/article/details/131252608