Search code examples
springcachinghazelcast

Cannot find cache named in hazelcast


i am using hazelcast version 5.1.5 and when i use spring @Cacheable("cache_name") on top of my method when i call the method exception throws.

java.lang.IllegalArgumentException: Cannot find cache named 'cache_name' for Builder[public java.util.List com.danial.user.management.service.UserService.getAllEntities2()] caches=[cache_name] | key='' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false';

here is my hazelcast configuration :

@Configuration
public class HazelcastConfig implements Serializable {

    @Value("${spring.application.name}")
    private String appName;

    @Bean
    public Config CacheConfig() {
        Config config = new Config();

        config.setInstanceName("hazelcast_mwga_cluster");

        config.getNetworkConfig().getJoin().getTcpIpConfig()
                .addMember("172.16.20.113")
                .setEnabled(true);


        // Configure a Hazelcast instance for each map.
        config.addMapConfig(createMapConfig("cache_name"));
        return config;

    }

    @Bean
    public HazelcastInstance hazelcastInstance() throws IllegalAccessException {
        return HazelcastClient.newHazelcastClient(createClientConfig());
    }

    private ClientConfig createClientConfig() throws IllegalAccessException {
        ClientConfig clientConfig = new ClientConfig();
        clientConfig.setClusterName("hazelcast_mwga_cluster");
        clientConfig.setInstanceName("hazelcast_mwga_cluster");
        clientConfig.addNearCacheConfig(createNearCacheConfig());
        return clientConfig;
    }

    private NearCacheConfig createNearCacheConfig() {

        NearCacheConfig nearCacheConfig = new NearCacheConfig();
        nearCacheConfig.setInvalidateOnChange(true);
        nearCacheConfig.setInMemoryFormat(InMemoryFormat.OBJECT);
        return nearCacheConfig;
    }

    private MapConfig createMapConfig(String name) {
        MapConfig mapConfig = new MapConfig(name);
        //do the special config here
        NearCacheConfig nearCacheConfig = new NearCacheConfig();
        nearCacheConfig.setInMemoryFormat(InMemoryFormat.OBJECT);
        nearCacheConfig.setInvalidateOnChange(true);
        nearCacheConfig.setEvictionConfig(new EvictionConfig());
        mapConfig.setNearCacheConfig(nearCacheConfig);

        return mapConfig;
    }
}

Solution

  • From the IllegalArgument exception, you can see that the cacheManager is empty.

    It should be an instance of HazelcastCacheManager. So the cache is not configured correctly.

    Also you are creating both Config bean and HazelcastInstance with a ClientConfig

    • Config is used for creating an Hazelcast cluster member
    HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(config);
    
    • ClientConfig is used for connecting to Hazelcast cluster as client
    HazelcastInstance hazelcastInstance = HazelcastClient.newHazelcastClient(clientConfig);
    

    Create only of these beans.