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

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

相关推荐
Logan Lie几秒前
Go 反射(Reflection)详解:从入门到实践
开发语言·后端·golang
superman超哥14 分钟前
Rust 异步性能的黑盒与透视:Tokio 监控与调优实战
开发语言·后端·rust·编程语言·rust异步性能·rust黑盒与透视·tokio监控与调优
Mr -老鬼25 分钟前
Rust 知识图谱 -进阶部分
开发语言·后端·rust
guchen6641 分钟前
CircularBuffer 优化历程:从数组越界到线程安全的完美实现
后端
古城小栈42 分钟前
Cargo.toml
开发语言·后端·rust
利刃大大1 小时前
【RabbitMQ】SpringBoot整合RabbitMQ:工作队列 && 发布/订阅模式 && 路由模式 && 通配符模式
java·spring boot·消息队列·rabbitmq·java-rabbitmq
AIGCExplore1 小时前
Jenkins 自动构建编译 Spring Boot 和 Vue 项目
vue.js·spring boot·jenkins
yangminlei1 小时前
Spring Boot 自动配置原理与自定义 Starter 开发实战
java·数据库·spring boot
悟空码字1 小时前
10分钟搞定!SpringBoot集成腾讯云短信全攻略,从配置到发送一气呵成
java·spring boot·后端
星浩AI1 小时前
从0到1:用LlamaIndex工作流构建Text-to-SQL应用完整指南
人工智能·后端·python