【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字段

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

相关推荐
Real_man13 分钟前
新物种与新法则:AI重塑开发与产品未来
前端·后端·面试
小马爱打代码41 分钟前
Spring Boot:将应用部署到Kubernetes的完整指南
spring boot·后端·kubernetes
卜锦元1 小时前
Go中使用wire进行统一依赖注入管理
开发语言·后端·golang
xinghunzhiye20101 小时前
redis升级
数据库·redis·缓存
kk在加油2 小时前
Redis基础数据结构
数据结构·数据库·redis
SoniaChen332 小时前
Rust基础-part3-函数
开发语言·后端·rust
代码的余温2 小时前
Spring Boot集成Logback日志全攻略
xml·spring boot·logback
全干engineer2 小时前
Flask 入门教程:用 Python 快速搭建你的第一个 Web 应用
后端·python·flask·web
没有bug.的程序员3 小时前
JAVA面试宝典 -《Spring Boot 自动配置魔法解密》
java·spring boot·面试
William一直在路上3 小时前
SpringBoot 拦截器和过滤器的区别
hive·spring boot·后端