RecipeInitial

Cache Warming on Startup

Pre-populate cache on application startup

Cache Warming on Startup

Pre-populate frequently accessed data on application startup to avoid cold-start latency.

Implementation

@Component
public class CacheWarmer implements ApplicationListener<ApplicationReadyEvent> {

    @Inject
    private CacheService cacheService;

    @Inject
    private CustomerService customerService;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        warmCustomerCache();
        warmConfigCache();
    }

    private void warmCustomerCache() {
        List<Customer> activeCustomers = customerService.findActive();
        for (Customer customer : activeCustomers) {
            cacheService.put(
                "customer:" + customer.getId(),
                customer,
                Duration.ofHours(1)
            );
        }
        log.info("Warmed {} customer records", activeCustomers.size());
    }
}

Best Practices

  • Only warm frequently accessed data
  • Use background threads for large datasets
  • Monitor startup time impact
  • Consider lazy warming for less critical data
  • About Cache Layer

    High-performance caching for qqq applications with Redis and in-memory support.

    View Full Documentation

    More Cache Layer Articles

    Powered by qqq