14.Redis之JAVASpring客户端

1.引入依赖

此时就会引入操作 redis 的依赖了~~

2.yml配置

复制代码
spring:
  redis:
    host: 127.0.0.1
    port: 8888

3.准备

  • 前面使用 jedis,是通过 Jedis 对象里的各种方法来操作 redis 的.
  • 此处Spring 中则是通过 StringRedisTemplate 来操作 redis .
  • 最原始提供的类是 RedisTemplate
  • StringRedisTemplate 是 RedisTemplate 的子类, 专门用来处理 文本数据的
  • 这个类提供的方法,相比于之前的 Jedis 中的各种方法,还是存在较大的差异的!!

4.代码

复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;


// 后续 redis 测试的各种方法, 都通过这个 Controller 提供的 http 接口来触发.
@RestController
public class MyController {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/testString")
    @ResponseBody
    public String testString() {
        redisTemplate.execute((RedisConnection connection) -> {
            // execute 要求回调方法中必须写 return 语句. 返回个东西.
            // 这个回调返回的对象, 就会作为 execute 本身的返回值.
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForValue().set("key", "111");
        redisTemplate.opsForValue().set("key2", "222");
        redisTemplate.opsForValue().set("key3", "333");

        String value = redisTemplate.opsForValue().get("key");
        System.out.println("value: " + value);

        return "OK";
    }

    @GetMapping("/testList")
    @ResponseBody
    public String testList() {
        // 先清除之前的数据.
        redisTemplate.execute((RedisConnection connection) -> {
            // execute 要求回调方法中必须写 return 语句. 返回个东西.
            // 这个回调返回的对象, 就会作为 execute 本身的返回值.
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForList().leftPush("key", "111");
        redisTemplate.opsForList().leftPush("key", "222");
        redisTemplate.opsForList().leftPush("key", "333");

        String value = redisTemplate.opsForList().rightPop("key");
        System.out.println("value: " + value);
        value = redisTemplate.opsForList().rightPop("key");
        System.out.println("value: " + value);
        value = redisTemplate.opsForList().rightPop("key");
        System.out.println("value: " + value);

        return "OK";
    }

    @GetMapping("/testSet")
    @ResponseBody
    public String testSet() {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForSet().add("key", "111", "222", "333");
        Set<String> result = redisTemplate.opsForSet().members("key");
        System.out.println("result: " + result);

        Boolean exists = redisTemplate.opsForSet().isMember("key", "111");
        System.out.println("exists: " + exists);

        Long count = redisTemplate.opsForSet().size("key");
        System.out.println("count: " + count);

        redisTemplate.opsForSet().remove("key", "111", "222");
        result = redisTemplate.opsForSet().members("key");
        System.out.println("result: " + result);

        return "OK";
    }

    @GetMapping("/testHash")
    @ResponseBody
    public String testHash() {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForHash().put("key", "f1", "111");
        redisTemplate.opsForHash().put("key", "f2", "222");
        redisTemplate.opsForHash().put("key", "f3", "333");

        String value = (String) redisTemplate.opsForHash().get("key", "f1");
        System.out.println("value: " + value);

        Boolean exists = redisTemplate.opsForHash().hasKey("key", "f1");
        System.out.println("exists: " + exists);

        redisTemplate.opsForHash().delete("key", "f1", "f2");

        Long size = redisTemplate.opsForHash().size("key");
        System.out.println("size: " + size);

        return "OK";
    }

    @GetMapping("/testZSet")
    @ResponseBody
    public String testZSet() {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForZSet().add("key", "zhangsan", 10);
        redisTemplate.opsForZSet().add("key", "lisi", 20);
        redisTemplate.opsForZSet().add("key", "wangwu", 30);

        Set<String> members = redisTemplate.opsForZSet().range("key", 0, -1);
        System.out.println("members: " + members);

        Set<ZSetOperations.TypedTuple<String>> membersWithScore = redisTemplate.opsForZSet().rangeWithScores("key", 0, -1);
        System.out.println("membersWithScore: " + membersWithScore);

        Double score = redisTemplate.opsForZSet().score("key", "zhangsan");
        System.out.println("score: " + score);

        redisTemplate.opsForZSet().remove("key", "zhangsan");

        Long size = redisTemplate.opsForZSet().size("key");
        System.out.println("size: " + size);

        Long rank = redisTemplate.opsForZSet().rank("key", "lisi");
        System.out.println("rank: " + rank);

        return "OK";
    }
}

5.测试

Spring Data Redis 中文文档 (springdoc.cn)

相关推荐
MaYuKang2 分钟前
「ES数据迁移可视化工具(Python实现)」支持7.x索引数据互传
大数据·数据库·python·mysql·elasticsearch
用户62799471826228 分钟前
南大通用GBase 8a数据库to_number关联报错问题处理方法
数据库
写bug写bug30 分钟前
Java并发编程:什么是线程组?它有什么作用?
java·后端
Andya_net36 分钟前
SpringBoot | 构建客户树及其关联关系的设计思路和实践Demo
java·spring boot·后端
申城异乡人38 分钟前
【踩坑系列】使用Comparator.comparing对中文字符串排序结果不对
java
Brian_Lucky40 分钟前
在 macOS 上合并 IntelliJ IDEA 的项目窗口
java·macos·intellij-idea
周杰伦_Jay42 分钟前
continue插件实现IDEA接入本地离线部署的deepseek等大模型
java·数据结构·ide·人工智能·算法·数据挖掘·intellij-idea
李小白6643 分钟前
数据库进阶之MySQL 程序
数据库·mysql
小博测试成长之路1 小时前
MongoDB技巧:快速找出重复字段记录
数据库·mongodb