Java项目启动时先加载某些方法可用于redis缓存预热

Java项目启动时先加载某些方法可用于redis缓存预热

业务场景:在系统启动后需要先加载某些方法,例如加载热点数据到redis进行缓存预热

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:开始运行...");
    }
}

执行顺序 @PostConstruct—>ApplicationRunner—>CommandLineRunner

缓存预热

1、定义

缓存预热就是在系统上线后,先加载某些热点key,防止出现缓存击穿

2、解决方案

1)手动写一个加载热点key的方法,上线后调用一下
2)数据量不大,可以在项目启动的时候自动进行加载。
3)通过定时任务刷新缓存。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值