在项目中使用 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);
        });
    }

}

后面如果有改变就继续写

相关推荐
郝学胜-神的一滴2 分钟前
Qt重复添加控件问题探析:现象、原理与解决方案
开发语言·数据库·c++·qt·程序人生
星空椰3 分钟前
Windows 安装 Oracle 19c Instant Client
数据库·windows·oracle
C++chaofan4 分钟前
JUC 并发编程从入门到精通(超详细笔记 + 实战案例)
java·jvm·spring boot·redis·后端·并发·juc
万象.5 分钟前
redis通用命令与数据结构
数据结构·数据库·redis
西柚小萌新7 分钟前
【大模型:RAG】--向量数据库Milvus详解2
数据库·milvus
小北方城市网7 分钟前
第 4 课:前端工程化进阶 ——Vue 核心语法 + 组件化开发(前端能力质的飞跃)
大数据·开发语言·数据库·python·状态模式·数据库架构
嵌入式×边缘AI:打怪升级日志17 分钟前
USB设备枚举过程详解:从插入到正常工作
开发语言·数据库·笔记
oMcLin17 分钟前
Ubuntu 22.04 系统通过 SSH 远程登录失败:如何解决 SSH 配置文件错误导致的登录问题
数据库·ubuntu·ssh
清水白石00822 分钟前
动态规划中的记忆化与缓存:原理、差异与 Python 实战指南
python·缓存·动态规划
代码游侠32 分钟前
复习——SQLite3 数据库
linux·服务器·数据库·笔记·网络协议·sqlite