'Spring Cache + Hibernate L2 cache via Hazelcast
I have Spring Boot application with Hibernate L2 cache enabled and I used a Hazelcast for this.
Also I want to add Spring cache, using @Cacheable annotation
I need to distribute this cache(spring cache and hibernate l2) between several pods in kubernetes, using embedded distributed cache pattern.
For now, I successfully distribute Hibernate l2 cache between pods using the following configuration
hazelcast.yaml
hazelcast:
instance-name: my-instance
network:
join:
multicast:
enabled: false
kubernetes:
enabled: true
namespace: dev
application.properties
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.url=jdbc:postgresql://host.docker.internal/postgres
spring.datasource.username=postgres
spring.datasource.password=pass
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.region.factory_class=com.hazelcast.hibernate.HazelcastCacheRegionFactory
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.show_sql=true
hibernate.cache.hazelcast.instance_name=my-instance
But I also need to share spring cache, using hazelcast. For example, I have the following code , and I want to distribute data, that cached in this service, between k8s pods
@Service
public class BookService {
@Autowired
private BookRepo bookRepo;
@Cacheable("books")
public Optional<Book> getBookById(int id) {
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
System.out.println("Book service triggered");
return bookRepo.findById(id);
}
}
And I have no idea How to correctly configure my application to share both spring and hibernate l2 cache between k8s pods ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
