java服务启动时提前预热_Spring启动缓存预热 (Spring boot cache warm up)

2017-04-21 14:19:33

0

I need some basic understanding of spring cache function. I want to build a cache warmer and resyncer as scheduled bean, in order to renew the cache on demand, when changes happen.

Let say, I have an AccountClient with a getAccount(String id), which fetches an Account from a very slow API. So basically I can do

@Cachable(cacheNames = "account", key = "#id", sync = true)

public Account getAccount(String id) {

//...

}

and everything works fine. Now I want to warm up the cache, and furthermore, I got a getNewOrChangedAccounts(), which retrieves IDs of changed accounts from my slow datastore.

so here is my approach:

public class AccountCacheManager {

//...

@Scheduled(initialDelay = 3000L, fixedRate = 10000L)

public void sync() {

List modifiedAccounts = accountClient.getNewOrChangedAccounts();

modifiedAccounts.getAccounts().parallelStream()

.forEach(account -> {

//delete old entry

evictAccount(account.getId());

//store new entry

putAccount(account.getId());

});

log.info("finished resync");

}

@CacheEvict(cacheNames = "account", key = "#id")

public void evictAccount(String id) {

log.debug("evicting account {}", id);

}

@CachePut(cacheNames = "account", key = "#id")

public void putAccount(String id) {

log.debug("storing account {}", id);

accountService.getAccount(id);

}

}

So I can proof, this process gets started and does something. However, when I hit my API, I see the first hit going to the slow backend, even if my sync goes over all entries from the backend.

I feel I misunderstood some details of spring caching API, so how can I achieve this?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值