当前位置: 首页 > article >正文

在项目中使用 redis存储 数据,提高 项目运行速度

在项目中发现从数据库中直接查找数据非常慢,所以想到了使用redis来加速

1. 引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

在yml配置中,设置redis地址

spring:
  redis:
    host: localhost
    port: 6379
    password: xxxxx
    database: 1
    lettuce:
      pool:
        # 连接池中最大空闲连接
        max-idle: 8
        # 连接池中最大阻塞等待时间(如果为负值则说明无限制)
        max-wait: -1
        # 连接池中最大的空闲连接数
        max-active: 8

 

2.  设置redis序列化器

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * 当前配置类不是必须的,因为 Spring Boot 框架会自动装配 RedisTemplate 对象,
 * 但是默认的key序列化器为JdkSerializationRedisSerializer,
 * 导致我们存到Redis中后的数据和原始数据有差别,故设置为StringRedisSerializer序列化器。
 */
@Configuration
public class RedisTemplateConfig {
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory){
        RedisTemplate<String ,String> redisTemplate = new RedisTemplate<>();//这里可以根据自己的需要修改,我因为键和值都是字符串类型的所以设置成了<String,String>
        redisTemplate.setConnectionFactory(connectionFactory);
        redisTemplate.setKeySerializer(RedisSerializer.string());//这里也需要修改
        return redisTemplate;
    }
}

3. 项目中使用

@Service
public class InspectionStationInfoServiceImpl extends ServiceImpl<InspectionStationInfoMapper, InspectionStationInfo>
    implements IInspectionStationInfoService {

    @Autowired
    private RedisTemplate redisTemplate;

     /**
     * 根据单位id,查询对应机构的首检合格率
     * @param unitId 单位id
     * @return 首检合格率
     */
    private String firstInspectionPassRate(String unitId){
        ValueOperations<String ,String> operations = redisTemplate.opsForValue();
        String key = String.format("SZJDC:CHECKRESULT:BGBH:%s",unitId);
        //在redis中获取 首检合格率
        String rate = operations.get(key);
        return rate;
    }

}

4. 设置定时任务来将数据存储进redis,规定每天凌晨1点更新

@Component
@Slf4j
public class FirstInspectionPassRateTask {
    @Autowired
    private CheckResultMapper checkResultMapper;

    @Autowired
    private InspectionStationInfoMapper inspectionStationInfoMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    //每天凌晨一点
    @Scheduled(cron = "0 0 1 * * ?")
    public void calculateFirstInspectionPassRate(){
        log.info("####### 定时任务开启......");
        //单位id 集合
        List<String> unitIdList = inspectionStationInfoMapper.selectUnitId();
        unitIdList = Optional.ofNullable(unitIdList).orElse(new ArrayList<>());
        ValueOperations<String ,String> operations = redisTemplate.opsForValue();

        unitIdList.forEach(unitId -> {
            //根据单位id,查询对应机构的首检合格率
            String rate = checkResultMapper.firstInspectionPassRate(unitId);
            String key = String.format("SZJDC:CHECKRESULT:BGBH:%s",unitId);
            operations.set(key,rate);
        });
    }

}

后面如果有改变就继续写


http://www.kler.cn/news/288904.html

相关文章:

  • 【Linux】 理解 Linux 中的 `dup2` 函数
  • Spring框架中的@EventListener注解浅谈
  • 【C++ Primer Plus习题】8.2
  • 直播路由器的原理是什么
  • Linux CentOS 7.39 安装mysql8
  • rabbitmq发送的消息接收不到
  • 告别文档处理烦恼,PDF Guru Anki一键搞定所有
  • 多目标应用:基于双存档模型的多模态多目标进化算法(MMOHEA)的移动机器人路径规划研究(提供MATLAB代码)
  • C语言之猜数字小游戏
  • 【苍穹外卖】Day3 菜品接口
  • dinput8.dll错误应该如何修复呢?五种快速修复dinput8.dll错误的问题
  • SpringBoot开发——初步了解SpringBoot
  • CephX 认证机制及用户管理
  • 功能测试常用的测试用例大全
  • 大模型入门 ch01:大模型概述
  • 强化学习,第 5 部分:时间差异学习
  • 数据结构——单链表相关操作
  • C# 开发环境搭建(Avalonia UI、Blazor Web UI、Web API 应用示例)
  • n*n矩阵,输出矩阵中任意两点之间所有路径
  • 使用组件库:提升开发效率的关键
  • Arduino library for proteus 下载 安装 测试
  • <数据集>TACO垃圾识别数据集<目标检测>
  • 编译与链接
  • ArrayList 和 LinkedList 之间的主要区别。在什么情况下你会选择使用 ArrayList 而不是 LinkedList,反之亦然?
  • 文本数据分析-(TF-IDF)(1)
  • 突发性网络攻击的安全挑战分析
  • Google Play下架超110万款应用,中国成重灾区
  • HiveQL如何统计用户近一段时间活跃标签
  • 设计模式 17 中介者模式
  • Spring优缺点和SpringBoot基础和搭建