1
votes

i'm getting an error while using java configuration for ehcache with spring boot and spring cache starter Cannot find cache named 'bpConfigs' for Builder[public java.util.List com.xxx.bp.repository.BpConfigRepository.getEligibleConfig()] caches=[bpConfigs] | key='' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'

my configuration :

@EnableCaching
@Configuration
public class CachingConfig  implements CachingConfigurer {

private static final int MAX_ENTRIES = 1000;
private static final int LIVE_IN_SEC = 10 * 60;

@Bean
public net.sf.ehcache.CacheManager ehCacheManager() {
    net.sf.ehcache.config.Configuration config = new Configuration();

 config.addCache(createBpConfigsCache());

    return net.sf.ehcache.CacheManager.newInstance(config);
}

@Bean
@Override
public CacheManager cacheManager() {
    return new EhCacheCacheManager(ehCacheManager());
}

@Bean
@Override
public CacheResolver cacheResolver()    {
    return new SimpleCacheResolver(cacheManager());
}

@Bean
@Override
public KeyGenerator keyGenerator() {
    return new SimpleKeyGenerator();
}

@Bean
@Override
public CacheErrorHandler errorHandler() {
    return new SimpleCacheErrorHandler();
}

private CacheConfiguration createBpConfigsCache() {

    CacheConfiguration cfg = new CacheConfiguration();

    cfg.setName("bpConfigs");
    cfg.setMemoryStoreEvictionPolicy("LRU");
    cfg.setTransactionalMode("OFF");
    cfg.setEternal(false);
    cfg.setTimeToLiveSeconds(LIVE_IN_SEC);
    cfg.setMaxEntriesLocalHeap(MAX_ENTRIES);

    return cfg;
}
}

My method :

@Repository
@CacheConfig(cacheNames ="bpConfigs")
public class BphConfigRepository {
@Cacheable
public List<LoyaltyEli> getbPConfig() {

    return jdbcTemplate.query("select XXX,YYY from TABLE", new myMapper());
}

}
1
your cache name is bphConfigs and your error message refers to bpConfigs. I think you have a typo at the point of definition or point of use. - Kevin
it's just my copy error the name is bpConfigs - Chakib Arrama
the usage site looks fine so I imagine its something about the @Configuration that's messed up. Typically the way I debug these things is to progressively simplify the configuration until it works. Here's a super simple example (not using ehcache) concretepage.com/spring-4/…. - Kevin
I used the SimpleCacheManager before this code and it works but my example with ehcache give the above error - Chakib Arrama

1 Answers

0
votes

I just removed the implemented interface CachingConfigurer and it's Okay now.

  @EnableCaching
  @Configuration("CachingConfig")
  public class CachingConfig {


  }