Certain methods are loaded first when a Java project is started, which can be used to warm up the redis cache.

Certain methods are loaded first when a Java project is started, which can be used to warm up the redis cache.

Business scenario: After the system starts, certain methods need to be loaded first, such as loading hotspot data to redis for cache preheating.

image-20230720172431788

image-20230720172445483

image-20230720172459912

image-20230720172354032

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

@Slf4j
@Service
public class FirstService {
    
    

    @PostConstruct
    public void test() {
    
    
        System.out.println("First-PostConstruct:开始运行...");
    }


}




import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

@Slf4j
@Service
public class TwoService implements CommandLineRunner {
    
    
    @Override
    public void run(String... args) throws Exception {
    
    
        System.out.println("Two-CommandLineRunner:开始运行...");
    }
}






import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class ThreeService implements ApplicationRunner {
    
    

    @Override
    public void run(ApplicationArguments args) throws Exception {
    
    
        System.out.println("Three-ApplicationRunner:开始运行...");
    }
}

Execution order @PostConstruct—> ApplicationRunner—>CommandLineRunner

Cache warm-up

1. Definition

Cache preheating is to load certain hotspot keys first after the system goes online to prevent cache breakdown.

2. Solution

1) Manually write a method for loading hotspot keys and call it after going online.
2) The amount of data is not large and can be loaded automatically when the project starts.
3) Refresh the cache through scheduled tasks.

Guess you like

Origin blog.csdn.net/itScholar001/article/details/131836022