Search code examples
javaspring-bootcachinghazelcast

Spring boot Cache Abstraction custom implementation


I have a use case, where I need to handle bulk gets and single queries in a spring boot application. Lately I realized that this can't be done with @Cacheable annotations as there partial miss scenarios can't be handled.

Is there any way I can directly access hazel cast cache instance and build a wrapper on top of that?

With this cache will be same but based on use case I can add @Cacheable or call custom implementation.


Solution

  • You can get a handle on the CacheManager, and then access the desired Cache objects like that:

    @Bean
    public CommandLineRunner run(CacheManager mgr) {
        return args -> {
            Cache cache = mgr.getCache("foo");
            // do something with cache
        };
    }
    

    Note the CommandLineRunner is just an example, but it shows how you can get a handle on the CacheManager, get the relevant Cache out of it, and inject it into your own bean.