SpringBoot Redis的使用

官方文档:

官方文档:Spring Data Redis :: Spring Data Redis

和jedis一样,SpringBoot Redis 也可以让我在Java代码中使用redis,同样也是通过引入maven依赖的形式。

加速访问github:

使用steam++可以免费加速访问github

SpringBoot Redis使用:

SpringBoot Redis:

创建springboot项目:

创建项目时勾选此依赖:

想在已有的springboot项目使用也可以直接添加依赖:

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

yml文件:

java 复制代码
spring:
  redis:
    host: 127.0.0.1
    port: 8888
server:
  port:8081

我们写的Java代码,会发送请求到127.0.0.1:8888然后被映射到云服务的端口,server.port:8080是我们springboot项目的访问端口,注意区分这两个端口的意义。

springboot redis代码

java 复制代码
@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";
    }
}
相关推荐
Long_poem13 分钟前
【自学笔记】Spring Boot框架技术基础知识点总览-持续更新
spring boot·笔记·后端
楠枬43 分钟前
网页五子棋——对战后端
java·开发语言·spring boot·websocket·spring
hong_zc1 小时前
SpringBoot 配置文件
java·spring boot·后端
早起的年轻人1 小时前
Docket Desktop 安装redis 并设置密码
数据库·redis·缓存
m0_748257462 小时前
创建一个简单的spring boot+vue前后端分离项目
vue.js·spring boot·后端
m0_748234902 小时前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
qw9492 小时前
Redis(高阶篇)03章——缓存双写一致性之更新策略探讨
数据库·redis·缓存
神马都会亿点点的毛毛张2 小时前
【SpringBoot教程】SpringBoot整合Caffeine本地缓存及Spring Cache注解的使用
java·spring boot·后端·spring·缓存·caffeine
zhyhgx2 小时前
【Spring】Spring配置文件
java·服务器·spring boot·后端·spring·配置文件