Jedis

Jedis操作Redis

Jedis使用的基本步骤:

  • 1.引入依赖
  • 2.Jedis连接池配置
  • 3.创建Jedis对象,建立连接
  • 4.使用Jedis,方法名与Redis命令一致
  • 5.释放资源

1.引入依赖

 <!-- jedis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.7.0</version>
        </dependency>
        <!-- 单元测试 -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.7.0</version>
            <scope>test</scope>
        </dependency>

2.Jedis连接池配置:

package com.starQeem.jedis.utils;

import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig;

public class JedisConnectionFactory { private static final JedisPool jedisPool;

static {
    //配置连接池
    JedisPoolConfig poolConfig = new JedisPoolConfig();
    poolConfig.setMaxTotal(8);
    poolConfig.setMaxIdle(8);
    poolConfig.setMinIdle(0);
    poolConfig.setMaxWaitMillis(1000);
    //创建连接池对象
    jedisPool = new JedisPool(poolConfig,&quot;106.53.103.231&quot;,6379,1000,&quot;1234&quot;);
}

public static Jedis getJedis(){
    return jedisPool.getResource();
}

}

- 3.创建Jedis对象,建立连接:

    private Jedis jedis;
@BeforeEach
void setUp() {
    //建立连接

// jedis = new Jedis("106.53.103.231", 6379); jedis = JedisConnectionFactory.getJedis(); //设置密码 jedis.auth("1234"); //选择库 jedis.select(0); }

- 4.使用Jedis,方法名与Redis命令一致:

 //测试String
    @Test
    void testString() {
        //插入数据,方法名就是redis命令名称
        String result = jedis.set("name", "张三");
        System.out.println("result:" + result);
        //获取资源
        String name = jedis.get("name");
        System.out.println(name);
    }
//测试hash
@Test
void testHash() {
    //插入数据
    jedis.hset(&quot;user:5&quot;, &quot;name&quot;, &quot;Qeem&quot;);
    jedis.hset(&quot;user:5&quot;, &quot;age&quot;, &quot;18&quot;);
    //获取资源
    System.out.println(jedis.hget(&quot;user:5&quot;, &quot;name&quot;));
    System.out.println(jedis.hget(&quot;user:5&quot;, &quot;age&quot;));
    System.out.println(jedis.hgetAll(&quot;user:5&quot;));

}

//测试list
@Test
void testList() {
    //插入数据
    jedis.lpush(&quot;star&quot;, &quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;);
    //获取资源
    System.out.println(jedis.lrange(&quot;star&quot;, 0, 5));
}

//测试set
@Test
void testSet() {
    //插入数据
    jedis.sadd(&quot;starQeem&quot;, &quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;);
    //获取资源
    System.out.println(jedis.smembers(&quot;starQeem&quot;));
}

//测试sorted set
@Test
void testSortedSet() {
    //插入数据
    jedis.zadd(&quot;starQeem2&quot;, 88, &quot;星星&quot;);
    jedis.zadd(&quot;starQeem2&quot;, 99, &quot;Qeem&quot;);
    jedis.zadd(&quot;starQeem2&quot;, 10, &quot;pig&quot;);
    //获取资源
    System.out.println(jedis.zscore(&quot;starQeem2&quot;, &quot;星星&quot;));
    System.out.println(jedis.zscore(&quot;starQeem2&quot;, &quot;Qeem&quot;));
    System.out.println(jedis.zscore(&quot;starQeem2&quot;, &quot;pig&quot;));
}

- 5.释放资源:

  //释放资源
    @AfterEach
    void tearDown() {
        if (jedis != null) {
            jedis.close();
        }
    }

end
Redis
SpringBoot

评论区

暂无评论