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

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

相关推荐
tan180°1 小时前
MySQL表的操作(3)
linux·数据库·c++·vscode·后端·mysql
DuelCode2 小时前
Windows VMWare Centos Docker部署Springboot 应用实现文件上传返回文件http链接
java·spring boot·mysql·nginx·docker·centos·mybatis
优创学社22 小时前
基于springboot的社区生鲜团购系统
java·spring boot·后端
why技术2 小时前
Stack Overflow,轰然倒下!
前端·人工智能·后端
幽络源小助理2 小时前
SpringBoot基于Mysql的商业辅助决策系统设计与实现
java·vue.js·spring boot·后端·mysql·spring
猴哥源码2 小时前
基于Java+springboot 的车险理赔信息管理系统
java·spring boot
Hello.Reader3 小时前
Redis 延迟排查与优化全攻略
数据库·redis·缓存
ai小鬼头3 小时前
AIStarter如何助力用户与创作者?Stable Diffusion一键管理教程!
后端·架构·github
简佐义的博客3 小时前
破解非模式物种GO/KEGG注释难题
开发语言·数据库·后端·oracle·golang