Spring Caching with HazelCast
HazelCast is a popular distributed caching solution, it provides Spring integration and also provides a
CacheManager
implementation.Get HazelCast
HazelCast provides commercial and open source version, download an open source copy from the community HazelCast website.
Unzip it into your local system. And run it.
Enable Caching
Add
@EnableCaching(mode=AdviceMode.ASPECTJ)
annotation on the configuration class if you are using Java based configuration.@Configuration ... @EnableCaching(mode=AdviceMode.ASPECTJ) public class JpaConfig {...}
Then, specify a
CacheManager
provider in your configuration.@Override @Bean public CacheManager cacheManager() { return new HazelcastCacheManager(hazelcastInstance()); } @Bean public HazelcastInstance hazelcastInstance() { ClientNetworkConfig networkConfig = new ClientNetworkConfig() .addAddress("localhost"); ClientConfig clientConfig = new ClientConfig(); clientConfig.setNetworkConfig(networkConfig); return HazelcastClient.newHazelcastClient(clientConfig); } @Override @Bean public KeyGenerator keyGenerator() { return new SimpleKeyGenerator(); }
Do not forget to add the hazelcast dependency in your Maven pom.xml file.
<dependency> <groupId>com.hazelcast</groupId> <artifactId>hazelcast</artifactId> </dependency>
Code sample
Check out the sample codes.
And explore the spring-cache-hazelcast folder.
评论