欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 游戏 > 在项目中使用 redis存储 数据,提高 项目运行速度

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

2025/12/18 10:28:40 来源:https://blog.csdn.net/2301_77615930/article/details/141709621  浏览:    关键词:在项目中使用 redis存储 数据,提高 项目运行速度

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

1. 引入依赖

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

在yml配置中,设置redis地址

spring:redis:host: localhostport: 6379password: xxxxxdatabase: 1lettuce: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 {@Beanpublic 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 {@Autowiredprivate 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 {@Autowiredprivate CheckResultMapper checkResultMapper;@Autowiredprivate InspectionStationInfoMapper inspectionStationInfoMapper;@Autowiredprivate 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);});}}

后面如果有改变就继续写

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词