【springboot】简易模块化开发项目整合Redis

接上一项目,继续拓展项目

1.整合Redis

添加Redis依赖至fast-demo-config模块的pom.xml文件中

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

在application.yml文件中增加Redis配置项

yaml 复制代码
# 数据库连接配置,记得新建一个数据库
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/my_demo?useUnicode=true&characterEncoding=UTF-8
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
  ## 注意是在spring下的,默认密码为空
  redis:
    host: 127.0.0.1
    # Redis服务器连接端口
    post: 6379
    jedis:
      pool:
        # 连接池最大连接数
        max-active: 100
        # 连接池中的最新空闲连接
        max-idel: 10
        # 连接池最大阻塞等待时间
        max-wait: 100000
      # 连接超时时间
      timeout: 5000
      # 默认是使用索引为0的数据库
      database: 0

新建RedisConfig类,封装Redis的一些方法以供后续使用

java 复制代码
@Component
public class RedisConfig {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 获取哈希字段的值
     * @param key
     * @return
     */
    public String get(final String key){
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * 新增一个字符串类型的值
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key,String value){
        boolean result = false;
        try {
            redisTemplate.opsForValue().set(key,value);
            result = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取原来key键对应的值并重新赋新值
     * @param key
     * @param value
     * @return
     */
    public boolean getAndSet(final String key,String value){
        boolean result = false;
        try {
            redisTemplate.opsForValue().getAndSet(key,value);
            result = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 判断哈希中是否存在指定的字段
     * @param key
     * @return
     */
    public boolean hasKey(final String key){
        boolean result = false;
        try {
            return redisTemplate.hasKey(key);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 删除指定的字段
     * @param key
     * @return
     */
    public boolean delete(final String key){
        boolean result = false;
        try {
            redisTemplate.delete(key);
            result = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 给指定字段设置有效时间
     * @param key
     * @param time  一天等于 1*24*60*60
     * @return
     */
    public boolean expire(final String key, long time){
        boolean result = false;
        try {
            return redisTemplate.expire(key, time, TimeUnit.SECONDS);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }
}

2.测试使用

启动Redis服务器和客户端

修改getUserList方法,将原本查询到的数据存入redis缓存中

java 复制代码
@RestController
@Api("用户信息接口")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisConfig redis;

    @RequestMapping(value = "/user",method = RequestMethod.GET)
    @ApiOperation(value = "获取所有用户信息", notes = "返回用户信息")
    public List<User> getUserList(){
        List<User> userList = userService.getUserList();
        redis.set("userList",userList.toString());
        return userList;
    }
}

运行后,在redis客户端输入keys *命令后,可见新增userList字段

内容比较少,就不贴完整代码了

相关推荐
t***265925 分钟前
SpringBoot + vue 管理系统
vue.js·spring boot·后端
qq_12498707531 小时前
基于springboot的疾病预防系统的设计与实现(源码+论文+部署+安装)
java·spring boot·后端·毕业设计
q***2512 小时前
Spring Boot 集成 Kettle
java·spring boot·后端
码事漫谈3 小时前
阿里《灵光》生成的视频下载不带水印的极简方法
后端
舒一笑3 小时前
信息的建筑学:MyBatis Log Panda 如何重构开发者的认知地图
后端·sql·intellij idea
码事漫谈3 小时前
WPF入门指南:解析默认项目结构
后端
iOS开发上架哦3 小时前
7种常见的源代码混淆技术详解:网络安全中的重要防线
后端
回家路上绕了弯3 小时前
单体架构拆微服务:从评估到落地的全流程指南
后端·微服务
疯狂的程序猴3 小时前
手游频繁崩溃闪退原因分析与iOS崩溃日志解析方法
后端
SoleMotive.3 小时前
redis实现漏桶算法--https://blog.csdn.net/m0_74908430/article/details/155076710
redis·算法·junit