Project startup and scheduled tasks in SpringBoot cache common database data into memory variables and convert them to high-frequency calls.

Scenes

In scheduled tasks, it is necessary to obtain data from the database, convert the data into the required format, and perform subsequent business processing.

The data in the database is not updated frequently.

The data in the database can be read once after the project is started, and then the database can be regularly queried to update the data through scheduled tasks.

There are many ways to implement database caching, such as the following:

In SpringBoot, database data is cached to Redis through custom cache annotations (AOP aspect interception):

In SpringBoot, database data is cached to the Redis_redis aspect through custom cache annotations (AOP aspect interception) - CSDN Blog

SpringBoot integrates Alibaba's open source cache access framework JetCache to implement declarative instance and method caching:

SpringBoot integrates Alibaba's open source cache access framework JetCache to implement declarative instance and method caching_springboot integrates jetcache-CSDN blog

Examples of usage, recycling, refreshing, statistics, etc. of Java tool library Guava local cache Cache:

Examples of usage, recycling, refreshing, statistics, etc. of Java tool library Guava local cache Cache - CSDN Blog

If the amount of data in msyql is not large or you do not want to introduce third-party middleware such as redis. This can be achieved in the following simple way.

Note:

Blog:
Domineering hooligan temperament_C#, architecture road, SpringBoot-CSDN blog

accomplish

1. Create a new scheduled task class

The method initBusRegion() reads data from the database and converts the data into the required format.

Declare a variable lineStrings to regularly receive and update the converted data from the database.

Then pass the variables to other scheduled task methods for subsequent business processing.

Sample code:

@Component("getDataTask")
@EnableScheduling
@Slf4j
public class GetDataTask {
   
    private List<LineString> lineStrings;

    @PostConstruct
    @Scheduled(cron = "0 0 * * * ?")
    public void initBusRegion() {

            QueryWrapper<BusRegionCoreEntity> qw = new QueryWrapper<>();
            List<BusRegionCoreEntity> busRegions = busRegionCoreMapper.selectList(qw);
            lineStrings = busRegions.stream().map(busRegion -> {
                double startX = bigDecimal2Double(busRegion.getStartX());
                double startY = bigDecimal2Double(busRegion.getStartY());
                double endX = bigDecimal2Double(busRegion.getEndX());
                double endY = bigDecimal2Double(busRegion.getEndY());
                LineString lineString = null;
                try {
                    lineString = GeometryUtil.createLineString(startX, startY, endX, endY);
                    lineString.setUserData(busRegion.getRegionName());
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
                return lineString;
            }).collect(Collectors.toList());
       
    }


    @Scheduled(cron = "0/1 * * * * ?")
    public void getData() {
         positionCar(lineStrings);
    }

    private Double bigDecimal2Double(BigDecimal bigDecimal) {
        if (bigDecimal == null) {
            return null;
        }
        return bigDecimal
                .setScale(15, RoundingMode.HALF_UP)
                .doubleValue();
    }
}

The sample code here is to use mybatisplus to read data from the table, then convert the x y fields from double to bigDecimal, and then call the tool class method

Map it into the data format required by List<LineString>.

Guess you like

Origin blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/134995948