Skip to content

Instantly share code, notes, and snippets.

@pledbrook
Created August 21, 2012 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pledbrook/3416150 to your computer and use it in GitHub Desktop.
Save pledbrook/3416150 to your computer and use it in GitHub Desktop.
Environment-specific cache managers in Spring
package org.example.config;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager
@Configuration
public class ApplicationConfig {
@Bean public ConcurrentMapCacheManager cacheManager() {
return new ConcurrentMapCacheManager();
}
}
package org.example.config;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
@Configuration
@Profile("cloud")
public class CloudApplicationConfig {
@Bean public JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setUsePool(true);
return factory;
}
@Bean public RedisTemplate redisTemplate() {
return new RedisTemplate(jedisConnectionFactory());
}
@Bean public RedisCacheManager cacheManager() {
return new RedisCacheManager(redisTemplate());
}
}
<web-app>
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>org.example.config.ApplicationConfig,org.example.config.CloudApplicationConfig</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>sampleServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
...
</web-app>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment