在项目中使用 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点更新

java 复制代码
@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);
        });
    }

}

后面如果有改变就继续写

相关推荐
RestCloud20 小时前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
RestCloud20 小时前
为什么说零代码 ETL 是未来趋势?
数据库·api
ClouGence1 天前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库
DemonAvenger1 天前
NoSQL与MySQL混合架构设计:从入门到实战的最佳实践
数据库·mysql·性能优化
AAA修煤气灶刘哥2 天前
别让Redis「歪脖子」!一次搞定数据倾斜与请求倾斜的捉妖记
redis·分布式·后端
AAA修煤气灶刘哥2 天前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
RestCloud2 天前
揭秘 CDC 技术:让数据库同步快人一步
数据库·api
得物技术2 天前
MySQL单表为何别超2000万行?揭秘B+树与16KB页的生死博弈|得物技术
数据库·后端·mysql
christine-rr2 天前
linux常用命令(4)——压缩命令
linux·服务器·redis
可涵不会debug2 天前
【IoTDB】时序数据库选型指南:工业大数据场景下的技术突围
数据库·时序数据库