Spring cache configuration

499 views
Skip to first unread message

geoff...@gmail.com

unread,
Feb 3, 2016, 5:49:18 AM2/3/16
to Hazelcast
I am trying to configure spring cache using hazelcast  such that cache entries will expire a fixed duration after they are created.

I tried the following, but the cache entries never seem to expire.  What am I missing? Does anyone have examples of configuring this sort of cache?

   @Bean
public CacheManager cacheManager(){
Config config=new Config();
final CacheSimpleConfig cacheConfig = new CacheSimpleConfig();

cacheConfig.setName("mycache");
cacheConfig.setExpiryPolicyFactoryConfig(new CacheSimpleConfig.ExpiryPolicyFactoryConfig(new CacheSimpleConfig.ExpiryPolicyFactoryConfig.TimedExpiryPolicyFactoryConfig(
CacheSimpleConfig.ExpiryPolicyFactoryConfig.TimedExpiryPolicyFactoryConfig.ExpiryPolicyType.CREATED, new CacheSimpleConfig.ExpiryPolicyFactoryConfig.DurationConfig(1,
TimeUnit.MINUTES))));
config.addCacheConfig(cacheConfig);

return new com.hazelcast.spring.cache.HazelcastCacheManager(Hazelcast.newHazelcastInstance(config));
}

Emrah Kocaman

unread,
Feb 3, 2016, 6:35:14 AM2/3/16
to haze...@googlegroups.com
You first need to decide which `CacheManager` implementation you'd like to use.

- If you'd like to use `com.hazelcast.spring.cache.HazelcastCacheManager` then your data will be cached in Hazelcast's `IMap`. So if you need to add expiration you need to add related map config. You don't need to add any `CacheSimpleConfig` as it won't be used.

Below configuration should be enough in your case.

  @Bean
public CacheManager cacheManager(){
Config config=new Config();
      MapConfig mapConfig = new MapConfig("mycache") //Must be the same name in your Cacheable annotation
      mapConfig.setTimeToLiveSeconds(60);
      config.addMapConfig(mapConfig);

return new com.hazelcast.spring.cache.HazelcastCacheManager(Hazelcast.newHazelcastInstance(config));
}

Please see spring-cache-manager code sample for details.

- If you'd like to use JCache as caching provider, then you need to use `JCacheCacheManager`. 




--
You received this message because you are subscribed to the Google Groups "Hazelcast" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hazelcast+...@googlegroups.com.
To post to this group, send email to haze...@googlegroups.com.
Visit this group at https://groups.google.com/group/hazelcast.
To view this discussion on the web visit https://groups.google.com/d/msgid/hazelcast/c9fec5ae-e271-4e5e-a0a3-c32c6269d5ad%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

geoff...@gmail.com

unread,
Feb 4, 2016, 5:18:48 AM2/4/16
to Hazelcast
This was very helpful.  Thank you.
Reply all
Reply to author
Forward
0 new messages