Reputation: 53
My spring config bean
@Bean
public Cache<String, Oauth2Token> Oauth2Cache(){
return Caffeine.newBuilder()
.initialCapacity(100)
.maximumSize(1000)
.expireAfter(new CustomExpiryOauth())
.build();
}
Below is my customeExpiry implementation
public class CustomExpiryOauth implements CustomExpiry<String, Oauth2Token> {
@Override
public long expireAfterCreate(String key, Oauth2Token value, long currentTime) {
log.info("expireAfterCreate value: {}", Long.parseLong(value.getExpiresIn())-2);
return TimeUnit.SECONDS.toNanos(Long.parseLong(value.getExpiresIn())-2);
}
@Override
public long expireAfterUpdate(String key, Oauth2Token value, long currentTime, long currentDuration) {
return currentDuration;
}
@Override
public long expireAfterRead(String key, Oauth2Token value, long currentTime, long currentDuration) {
return currentDuration;
}
}
Below is defined in my service class. I will get expiry time when getOauth2Token() is invoked which I put in the cache. Defined Lambda function thinking that it will helps refresh the cache when it is expired.
@Cacheable(cacheNames = "oauth2Cache" ,key = "#myMap.get('systemName')")
public Oauth2Token getOauth2TokenFromCache(Map<String, String> myMap) {
log.info("At getOauth2TokenFromCache expiresIn = {} ",getOauth2Token(myMap).getExpiresIn());
return oauth2Cache.get(myMap.get("systemName"), k -> getOauth2Token(myMap));
}
When the cache entry expires, does that mean the oauth2Cache.get(keyString) will return null and CustomExpiryOauth.expireAfter() will be called to refresh the cache. This is not happening when cache entry expires, also I get below error when customeExpiry is invoked on adding new entry. What is wrong here? Any help is appreciated.
Upvotes: 0
Views: 225