我有一个用例,我配置了多个具有不同属性的缓存管理器,以及用单独的缓存名称注释的不同方法。缓存的方法是从 http 客户端异步检索数据,并缓存响应。在上述用例中,来自两个缓存方法的数据在返回结果之前被合并。有时,结果仅包含来自缓存方法之一的数据,并且在刷新时问题得到解决。我无法理解在什么情况下会出现问题?
@Configuraions
public class CacheConfig{
public static final String CACHE1 = "cache1";
public static final String CACHE2 = "cache2";
@Value("${cache.caffeineSpec:expireAfterWrite=43200s,maximumSize=1000,recordStats}")
private String cacheSpec1;
@Value("${cache.caffeineSpec: expireAfterWrite=3600s,maximumSize=2000,recordStats}")
private String cacheSpec2;
@Bean("cacheManager1")
@Primary
public CacheManager brokerDetailscacheManager() {
CaffeineCacheManager cacheManager = new CaffeineCacheManager(CACHE1);
cacheManager.setCaffeine(Caffeine.from(cacheSpec1));
return cacheManager;
}
@Bean("cacheManager2")
public CacheManager brokerTierCodeMapCacheManager() {
CaffeineCacheManager cacheManager = new CaffeineCacheManager(CACHE2, BROKER_TIER_CACHE);
cacheManager.setCaffeine(Caffeine.from(cacheSpec2));
return cacheManager;
}
}
正在使用的型号
public class Person {
private String firstname;
private String lastname;
private List<Address> adresses;
}
private class Address {
private String street;
private String City
private String zip;
}
private class PersonInfo {
private String firstname;
private String lastname;
private Address address;
}
缓存的方法类是:
@Service
@RequiredArgsConstructor
public class PersonCache {
private final DataClient dataClient;
@Cacheable(cacheNames = CacheConfig.CACHE1, cacheManager = "cacheManager1" ,sync = true)
public Map<String, Person> getPersonDetails(String firstname) {
Map<String, Person> personMap = new HashMap()<>;
//Key is first name, grouping all results by firstname
try {
personMap = dataClient.getPersonDetails(firstname)
.toCompletableFuture()
.get(3, TimeUnit.SECONDS);
}catch(Exception e) {
log.error("Error fetching response from api". e);
}
}
@Cacheable(cacheNames = CacheConfig.CACHE2, cacheManager = "cacheManager2" ,sync = true)
public Map<String, Person> getPersonDetails(String firstname) {
List<PersonInfo> personMap = new ArrayList();
try {
personMap = dataClient.getPersonInfoDetails(firstname)
.toCompletableFuture()
.get(3, TimeUnit.SECONDS);
}catch(Exception e) {
log.error("Error fetching response from api". e);
}
return transformPersonInfoToPerson(personMap);
}
}
调用方法:
@Service
@RequiredArgsConstructor
public class PersonService {
private final PersonCache personCache;
public List<Person> getPersonDetails(String firstName) {
Map<String, Person> personResponse1 = personCache.getPersonDetails(firstName);
//.. after fetching for the first result set, check for a flag and call the below cache to append the data
Map<String, Person> personResponse2 = personCache.getPersonInfoDetails(firstName);
personResponse1.putAll(personResponse2);
// This when returned at times does not contain any response from personResponse1 and only contains the personResponse2 data
return personResponse1.values();
}
}
异步 API 调用是否可能导致某种未命中,并且第二个缓存的结果集被添加到结果中并返回?(调用方法也是从控制器类异步调用的)
无论端点被触发的次数如何,我应该如何处理以获得一致的响应?