Spring @Cacheable annotation not working with caffeine cache

105 Views Asked by At

I'm trying to get @Cacheable annotation working on one of my cache service class. Cache manager class

@Configuration
@EnableCaching
public class CaffeineCacheManager extends CachingConfigurerSupport{
    @Bean
    public CacheManager cacheManager(){
        CaffeineCacheManager cacheManager = new CaffeinieCacheManager("appConfigCache","sysConfigCache");
        cacheManager.setCaffeine(
            Caffeine.newBuilder()
                .initialCapacity(300)
                .maximumSize(1000)
                .recordStats()
            );
        return cacheManager;
    }
}

I have two cache service class

@Service
@CacheConfig(cacheName={"appConfigCache"})
public class AppConfigurationCacheService{
    @Autowired
    private AppConfigurationDao appConfigurationDao;
    
    @Cacheable(key="#appConfigKey")
    public AppConfiguration getAppConfig(String appConfigKey){
        AppConfiguration appConfig = appConfigurationDao.getAppConfigurationByKey(appConfigKey);
        return appConfig;
    }
}
@Service
@CacheConfig(cacheName={"sysConfigCache"})
public class SysConfigurationCacheService{
    @Autowired
    private SysConfigurationDao sysConfigurationDao;
    
    @Cacheable(key="#configKey")
    public SysConfiguration getSysConfig(String configKey){
        
        SysConfiguration sysConfig = sysConfigurationDao.getSysConfigurationByKey(configKey);
        return sysConfig;
    }
}

When I execute loadApplicationConfig from the following class it will always retrieve it from the database using AppConfigurationDao class

@Service
public class LoadApplicationConfig{
    
    @Autowired
    private AppConfigurationCacheService appCacheService;
    
    public void loadApplicationConfig(String configName){
        //This method does not retrieve object from cache but from the database
        AppConfiguration appConfig = appCacheService.getAppConfig(configName);
    }
}

@Cacheable works on SysConfigurationCacheService class. It will retrieve the object from the cache. Also in SysConfigurationCacheService if I put different SpEL like key"#configKey" it will throw an exception. However, in AppConfigurationCacheService it does not throw any exception. I've been looking at this for couple of days and I can't figure out why caffeine cache is not working on AppConfigurationCacheService class. I desperately need some insight on this issue.

Thank you in advance

0

There are 0 best solutions below