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

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

相关推荐
trayvontang12 分钟前
Spring属性自动配置原理与自定义转换
spring boot·spring·spring属性自动配置·spring转换原理·spring自定义属性转换器
SimonKing12 分钟前
MyBatis的隐形炸弹:selectByExampleWithBLOBs使用不当,让性能下降80%
java·后端·程序员
袁煦丞 cpolar内网穿透实验室13 分钟前
无需公网 IP 也能全球访问本地服务?cpolar+Spring Boot+Vue应用实践!
vue.js·spring boot·tcp/ip·远程工作·内网穿透·cpolar
踏浪无痕17 分钟前
告别 Grafana 手搓 Dashboard:基于指标分组的 Prometheus 可视化新方案
后端·架构·产品
天天摸鱼的java工程师18 分钟前
分布式 ID 生成终极方案:雪花算法优化与高可用实现
java·后端
掘金者阿豪20 分钟前
Jenkins 任务中的 `java.lang.InterruptedException` 异常解析与解决
后端
superman超哥22 分钟前
Rust 零拷贝技术应用:极致性能的内存操作艺术
开发语言·后端·rust·rust零拷贝技术·内存操作
间彧23 分钟前
深度解析AIOps:从架构设计到工具实践的智能运维体系
后端
superman超哥24 分钟前
Rust SIMD 指令优化:数据并行的极致性能
开发语言·后端·rust·数据并行·指令优化
幽络源小助理26 分钟前
SpringBoot+Vue雅苑小区管理系统源码 | Java物业项目免费下载 – 幽络源
java·vue.js·spring boot