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

商业网站开发文档国内优秀网页网站

商业网站开发文档,国内优秀网页网站,建设微信网站的流程图,网站建设团队技术介绍在 Spring 应用启动过程中进行缓存预热(Cache Preloading)是一种优化手段,目的是在系统正式对外提供服务前,提前加载高频访问的数据到缓存(如 Redis、Caffeine 等),避免用户首次请求时因缓存未命…

在 Spring 应用启动过程中进行缓存预热(Cache Preloading)是一种优化手段,目的是在系统正式对外提供服务前,提前加载高频访问的数据到缓存(如 Redis、Caffeine 等),避免用户首次请求时因缓存未命中(Cache Miss)导致性能下降。以下是详细的实现方案和最佳实践:

1. 缓存预热的核心思路

  • 目标:在 Spring 容器初始化完成后,主动加载热点数据到缓存。

  • 适用场景

    • 高频访问的静态数据(如配置表、城市列表)。

    • 计算成本高的数据(如排行榜、聚合统计结果)。

  • 关键时机:确保预热在应用完全启动后执行,且不影响正常服务。


2. 实现方案

方案1:使用 @PostConstruct 注解或者实现 InitializingBean 接口,
 实现 InitializingBean 接口,可以重写afterPropertiesSet 方法中执行缓存预热的逻辑。这样,Spring 在初始化 Bean 时会调用 afterPropertiesSet 方法。

在 Spring Bean 初始化完成后立即执行预热逻辑:

@Service
public class CacheWarmUpService {@Autowiredprivate UserService userService; // 依赖的业务服务@Autowiredprivate CacheManager cacheManager; // Spring 缓存管理器@PostConstruct  // 在 Bean 初始化后执行public void warmUpCache() {List<User> hotUsers = userService.getTopActiveUsers(100); // 加载热点数据hotUsers.forEach(user -> cacheManager.getCache("userCache").put(user.getId(), user));}
}

import org.springframework.beans.factory.InitializingBean;

import org.springframework.stereotype.Component;

@Component

public class CachePreloader implements InitializingBean {

                 @Override

                public void afterPropertiesSet() throws Exception {

                        // 执行缓存预热逻辑

                        // ...

                }

}

优点:简单直接,适合小规模预热。
缺点:可能阻塞 Spring 启动流程,需控制预热时间。


方案2:实现 ApplicationListener 监听上下文就绪事件

ApplicationReadyEvent 是 Spring Boot 框架中的一个事件类,它表示应用程序已经准备好接收请求,即应用程序已启动且上下文已刷新。这个事件是在 ApplicationContext 被初始化和刷新,并且应用程序已经准备好处理请求时触发的。

基于ApplicationReadyEvent,我们可以在应用程序完全启动并处于可用状态后执行一些初始化逻辑。使用 @EventListener 注解或实现 ApplicationListener 接口来监听这个事件。

在 Spring 上下文完全初始化后触发预热:

@Component
public class CacheWarmUpListener implements  ApplicationListener<ContextRefreshedEvent> {
//通过实现ApplicationListener<ContextRefreshedEvent>接口
//也可以通过@EventListener实现@Overridepublic void onApplicationEvent(ContextRefreshedEvent event) {if (event.getApplicationContext().getParent() == null) { // 避免子容器重复执行// 执行预热逻辑warmUpRedisCache();warmUpLocalCache();}}private void warmUpRedisCache() {// 例如:预加载商品数据到 Redis// ...}
}

优点:确保所有 Bean 已就绪,适合复杂预热逻辑。
缺点:需注意避免重复执行(如 Web 应用可能有父子容器)。


方案3:使用 CommandLineRunner 或 ApplicationRunner

Spring Boot 提供的启动后扩展点:

@Component
@Order(1) // 控制执行顺序
public class CacheWarmUpRunner implements CommandLineRunner {@Autowiredprivate ProductService productService;@Overridepublic void run(String... args) {List<Product> hotProducts = productService.getHotProducts();// 写入缓存(如 Caffeine、Redis)}
}

优点:与 Spring Boot 生命周期无缝集成,支持多任务顺序控制。
缺点:仅适用于 Spring Boot 项目。


方案4:异步预热(推荐)

避免阻塞主线程,使用 @Async 异步执行:

@Service
public class AsyncCacheWarmUpService {@Async  // 需启用 @EnableAsyncpublic void warmUpCacheAsync() {// 耗时预热逻辑(如全量加载数据库数据到缓存)}
}// 通过监听器或 Runner 触发异步任务
@Component
public class CacheWarmUpTrigger implements ApplicationListener<ContextRefreshedEvent> {@Autowiredprivate AsyncCacheWarmUpService asyncService;@Overridepublic void onApplicationEvent(ContextRefreshedEvent event) {asyncService.warmUpCacheAsync();}
}

优点:不阻塞应用启动,适合大数据量预热。
注意:需配置线程池(通过 ThreadPoolTaskExecutor)。


3. 结合具体缓存框架

3.1 Redis 预热
public void warmUpRedis() {StringRedisTemplate redisTemplate = ...;List<City> cities = cityRepository.findAll();cities.forEach(city -> redisTemplate.opsForValue().set("city:" + city.getId(), city.getName()));
}
3.2 Caffeine 本地缓存预热
@Bean
public Cache<String, Product> productCache() {return Caffeine.newBuilder().maximumSize(1000).build(cache -> {// 启动时自动加载数据return productService.getProductById(cache.key());});
}

4. 最佳实践

  1. 分批次加载:避免单次加载数据量过大导致 OOM 或超时。

    List<User> users = userService.getAllUsers();
    int batchSize = 100;
    for (int i = 0; i < users.size(); i += batchSize) {List<User> batch = users.subList(i, Math.min(i + batchSize, users.size()));batch.forEach(user -> cache.put(user.getId(), user));
    }
  2. 动态调整预热策略:通过配置文件控制是否启用预热或选择预热数据集。

    cache:warm-up:enabled: truedata-sets: top_users,hot_products
  3. 监控与日志:记录预热耗时和数据量,便于优化。

    @Slf4j
    public class CacheWarmUpRunner implements CommandLineRunner {@Overridepublic void run(String... args) {long start = System.currentTimeMillis();// 预热逻辑log.info("Cache warm-up completed in {} ms", System.currentTimeMillis() - start);}
    }

5. 避免的坑

  • 循环依赖:预热 Bean 不要依赖其他未初始化的 Bean。

  • 事务问题确保预热方法内数据库操作已提交(可加 @Transactional)。

  • 分布式环境在集群中仅由一个节点执行预热(通过分布式锁控制)。


总结

方案适用场景是否阻塞启动
@PostConstruct简单、小数据量预热
ApplicationListener需要完整上下文
CommandLineRunnerSpring Boot 项目,需控制顺序
异步预热(推荐)大数据量或耗时任务

选择合适方案后,结合具体缓存框架(Redis/Caffeine)和业务需求,可显著提升系统启动后的缓存命中率。


文章转载自:

http://CJ6Yn4zw.sfphz.cn
http://cOWbyL0h.sfphz.cn
http://SkNt9b38.sfphz.cn
http://L2ORxoT0.sfphz.cn
http://lM5400sA.sfphz.cn
http://cGFeFIKo.sfphz.cn
http://UVLZx1GV.sfphz.cn
http://en6vc5Ad.sfphz.cn
http://QPsY2frl.sfphz.cn
http://PLFohWWZ.sfphz.cn
http://eGIUhsW8.sfphz.cn
http://MnONDDT3.sfphz.cn
http://zTWFmtcg.sfphz.cn
http://bEUjYr7b.sfphz.cn
http://8Z3RwLpc.sfphz.cn
http://f2sE0x7H.sfphz.cn
http://Q8MLugZP.sfphz.cn
http://5Mswev4K.sfphz.cn
http://zxqV4He9.sfphz.cn
http://ynbFgFHe.sfphz.cn
http://qc2fTvuy.sfphz.cn
http://6kCQ1FZt.sfphz.cn
http://GjS6YTCC.sfphz.cn
http://QjC9hGOl.sfphz.cn
http://Ua4gnzvn.sfphz.cn
http://wImxdZWA.sfphz.cn
http://LKcVK2fa.sfphz.cn
http://KOTVPot4.sfphz.cn
http://aYChKma4.sfphz.cn
http://weGQlNth.sfphz.cn
http://www.dtcms.com/wzjs/715227.html

相关文章:

  • 矢量插画的网站做网站用js的好处
  • 什么app做网站做窗帘什么网站
  • 当阳网站建设网站接入支付宝需要网站备案吗
  • 建水网站建设极限优化wordpress
  • 盐城网站建设找宇wordpress 淘宝客
  • 怎么建商城网站吗wordpress长文章不显示评论框
  • 中山手机网站设计重庆承越网站制作公司
  • 做网站怎么切片老薛主机 wordpress 打不开
  • 包头网站建设推广在线制图网页版
  • 创新的南昌网站建设广州网站建设选哪家
  • 苏州网站排名推广新买的服务器怎么做网站
  • 前后端分离的网站怎么做wordpress站点标题美化
  • 网站开发教程pdf网站开发命名规则
  • 东莞公司品牌网站建设网站开发形式有哪些
  • 傻瓜做网站用什么软件家具设计作品
  • 产品网站系统可以做设计私单的网站
  • 番禺做网站公司怎么做电影网站不违法吗
  • 文件注入网站网站功能建设特点
  • 西凤酒网站建设的目标异构国际设计
  • 佛山专业的免费网站优化网站制作有哪些技术
  • 网站外包建设wordpress添加前台
  • phpcms 网站模板网页制作与网站建设实验报告
  • 网站防采集 如何采集管理咨询公司一般是做什么的
  • 站酷网首页wordpress下载主题模板
  • 河南睢县筑宇建设网站wordpress广告位的添加方法
  • 绍兴市中等专业学校网站镇江网站建设推广公司
  • 环保网站建设说明网站网页设计0基础学
  • iis更改默认网站百度网站推广怎么样
  • 仿70网站分类目录源码凡客平台
  • wordpress 增加站长统计2022装修简约风格效果图