Redis(3.17)

1

2

3

4

5

6

七、jedis基本使用

1.导入Jedis相关的依赖:

<dependencies>
    <!-- jedis 链接 redis -->
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.1</version>
    </dependency>
    <!-- 测试 -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>     
</dependencies>

2.普通API:

public class Jedistest {
    @Test
    public void testRedis() {
        //1.获取 jedisPool 对象
        JedisPool jedisPool = new JedisPool("localhost",6379);
        //2.在 jedisPool 连接池中获取 jedis 连接对象
        Jedis jedis = jedisPool.getResource();
        //3.设置密码(若需要)
        jedis.auth("123456");
        //4.书写 redis 代码 TODO
        //String 类型
        jedis.set("str","String类型");
        //Hash 类型
        jedis.hset("user","name","xiaoming");
        jedis.hset("user","age","10");
        jedis.hincrBy("user","age",10);
        //List 类型
        jedis.rpush("arr","1","2","3");
        String number = jedis.lpop("arr");
        System.out.println(number);
        //Set 类型
        jedis.sadd("hobbys","Java","Python","C","C++","JavaScript");
        Set<String> hobbys = jedis.spop("hobbys",1);
        System.out.println(hobbys);
        //Zset 类型
        jedis.zadd("z",100,"小芳");
        jedis.zadd("z",90,"小明");
        jedis.zadd("z",95,"小李");
        //5.关闭连接
        jedis.close();
        //6.销毁连接池对象
        jedisPool.destroy();

    }
}

八、集成SpringBoot

1.导入依赖:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>
        <relativePath/>
</parent>


<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
</dependencies>

2.application.yml配置文件:

spring:
  redis:
    host: localhost
    port: 6379
    password: 123456

3.API:

@SpringBootTest
public class RedisSpringBootTest {

    @Autowired
    private StringRedisTemplate template;

    @Test
    public void myTest() {
        // 操作string opsForValue().xx()
        template.opsForValue().set("number","5");
        template.opsForValue().increment("number");
        // 操作hash opsForHash().xx()
        template.opsForHash().put("user02","name","小明");
        template.opsForHash().put("user02","age","18");// 不能直接输入数字
        template.opsForHash().increment("user02","age",2);
        // 操作list opsForList().xx()
        template.opsForList().rightPushAll("1","2","3","4","5");
        System.out.println(template.opsForList().rightPop("arr"));
        // 操作set opsForSet().xx()
        template.opsForSet().add("hobbys","Java","Python","C++","C","JavaScript");
        System.out.println(template.opsForSet().pop("hobbys",1));
        // 操作zset opsForZSet().xx()
        template.opsForZSet().add("score","张三",90);
        template.opsForZSet().add("score","李四",100);
        template.opsForZSet().add("score","王五",95);
        Set<String> strings = template.opsForZSet().reverseRange("score",0,-1);
        if(strings != null) {
          strings.forEach(System.out::println);
        }
        //通用命令
        Set<String> keys = template.keys("*");
        keys.forEach(System.out::println);
    }
    @Test
    public void testTool(){
        GenericObjectPoolConfig config = new GenericObjectPoolConfig();
        ////最大连接数, 默认8个
        config.setMaxTotal(100);
        ////最大空闲连接数, 默认8个
        config.setMaxIdle(20);
        //获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间,  默认-1
        config.setMaxWaitMillis(-1);
        //在获取连接的时候检查有效性, 默认false
        config.setTestOnBorrow(true);
        JedisPool pool = new JedisPool(config,"192.168.122.128",6379,5000,"wolfcode");
        Jedis j = pool.getResource();
        String name = j.get("name");
        System.out.println(name);
        j.close();
        pool.close();
        pool.destroy();
    }

}
posted on 2026-03-17 19:30  冬冬咚  阅读(4)  评论(0)    收藏  举报