SpringBoot 集成Redis

SpringBoot 集成Redis

  1. 添加 redis 依赖
    在这里插入图片描述
    或将以下配置添加到 pom.xml 中:

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

  1. 配置 redis
spring.redis.database=0 
spring.redis.port=6379 
spring.redis.host=82.157.146.10
# 可省略
spring.redis.lettuce.pool.min-idle=5 
spring.redis.lettuce.pool.max-idle=10 
spring.redis.lettuce.pool.max-active=8 
spring.redis.lettuce.pool.max-wait=1ms 
spring.redis.lettuce.shutdown-timeout=100ms
  1. ⼿动操作 redis
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
public class RedisController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
 // 在 redis 存储数据
    @RequestMapping("/setrs")
    public String setRedis(String name, String value) { 
        stringRedisTemplate.opsForValue().set(name, value, 
                30, TimeUnit.SECONDS);
        return "Set redis success.";
    }
 // 读取 redis 中的数据
    @RequestMapping("/getrs")
    public String getRedis(String name) {
        Object valObj = stringRedisTemplate.opsForValue().get(name); 
        if (valObj != null) return valObj.toString();
        return "Null";
    }
}
  1. 注解操作 redis

<<1>> 开启缓存


@SpringBootApplication
@EnableCaching

<<2>> 注解操作 redis

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;

@RestController
public class RedisAnnotationController {

    @Cacheable(value = "spring:cache", key = "#name") 
    @RequestMapping("/setrs")
    public String setRedis(String name, String value) {
        if (!StringUtils.hasLength(name) || !StringUtils.hasLength(value)) 
 {
            return "请先输⼊ name 和 value";
        }
        return value;
    }

    @RequestMapping("/uprs")
    @CachePut(value = "spring:cache", key = "#name")
    public String updateRedis(String name, String value) {
        if (!StringUtils.hasLength(name) || !StringUtils.hasLength(value)) 
 {
            return "请先输⼊ name 和 value";
        }
        return value;
    }

    @RequestMapping("/delrs")
    @CacheEvict(value = "spring:cache", key = "#name") 
    public String delUser(String name) {
        return "delete success";
    }
}

相关推荐

  1. SpringBoot集成Redis

    2024-04-24 07:40:02       24 阅读
  2. springboot集成-Redis

    2024-04-24 07:40:02       30 阅读
  3. springboot项目集成Redis,使用redis各项功能

    2024-04-24 07:40:02       48 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-24 07:40:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-24 07:40:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-24 07:40:02       82 阅读
  4. Python语言-面向对象

    2024-04-24 07:40:02       91 阅读

热门阅读

  1. spispi

    2024-04-24 07:40:02       33 阅读
  2. IOS 设置UIButton按钮的选中状态样式

    2024-04-24 07:40:02       33 阅读
  3. 利用HttpClient库下载蚂蜂窝图片

    2024-04-24 07:40:02       35 阅读
  4. Ant Design Pro + springboot实现文件上传功能

    2024-04-24 07:40:02       33 阅读
  5. 算法设计与优化——向量中数据唯一化

    2024-04-24 07:40:02       32 阅读
  6. K8s: 控制器之StatefulSets对象

    2024-04-24 07:40:02       28 阅读
  7. flutter 解决ExpandableText组件三个点调整颜色问题

    2024-04-24 07:40:02       30 阅读