Skip to content

Instantly share code, notes, and snippets.

@denov
Created June 9, 2016 16:48
Show Gist options
  • Save denov/021e9c736d15b1fc56c3e8d185b8ef15 to your computer and use it in GitHub Desktop.
Save denov/021e9c736d15b1fc56c3e8d185b8ef15 to your computer and use it in GitHub Desktop.
package org.magic.config;
import org.magic.config.annotation.Aws;
import org.magic.config.annotation.LocalDev;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.AdviceMode;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@Configuration
@PropertySource(value = { "classpath:spring/application.${env:local}.properties" })
public class CacheConfig {
public static final long HALF_HOUR = 1800l;
public static final long ONE_HOUR = 3600l;
public static final long TWO_HOURS = 7200l;
public static final String CACHE_GUIDELINES_ALL = "guidelines";
public static final String CACHE_GUIDELINE_LAST_PUBLISHED = "guidelines_publish";
public static final String CACHE_GUIDELINES_PUBLIC = "guidelines_public";
public static final String CACHE_GUIDELINE_PERMISSIONS = "guideline_permissions";
public static final String CACHE_GUIDELINE = "guideline";
public static final String CACHE_PICO = "pico";
public static final String CACHE_PICO_OUTCOME_C = "c_outcome";
public static final String CACHE_PICO_OUTCOME_D = "d_outcome";
public static final String CACHE_PICO_OUTCOME_NP = "np_outcome";
public static final String CACHE_PICO_ISSUES = "issues";
public static final String CACHE_RECOMMENDATION = "recommendations";
public static final String CACHE_I18N = "i18n";
private static final Logger log = LoggerFactory.getLogger(CacheConfig.class);
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
@LocalDev
@Configuration
@EnableCaching(mode=AdviceMode.ASPECTJ)
public static class local {
public local() {
log.info("local cache config");
}
@Bean
public CacheManager cacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache(CACHE_I18N),
new ConcurrentMapCache(CACHE_PICO),
new ConcurrentMapCache(CACHE_PICO_OUTCOME_C),
new ConcurrentMapCache(CACHE_PICO_OUTCOME_D),
new ConcurrentMapCache(CACHE_PICO_OUTCOME_NP),
new ConcurrentMapCache(CACHE_PICO_ISSUES),
new ConcurrentMapCache(CACHE_RECOMMENDATION),
new ConcurrentMapCache(CACHE_GUIDELINE),
new ConcurrentMapCache(CACHE_GUIDELINES_ALL),
new ConcurrentMapCache(CACHE_GUIDELINE_LAST_PUBLISHED),
new ConcurrentMapCache(CACHE_GUIDELINES_PUBLIC),
new ConcurrentMapCache(CACHE_GUIDELINE_PERMISSIONS)));
return cacheManager;
}
}
@Aws
@Configuration
@EnableCaching(mode=AdviceMode.ASPECTJ)
public static class aws {
@Value("${aws.redis.cache}")
protected String redisCluster;
public aws() {
log.info("AWS cache config");
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
log.info(" * AWS redis cache @ {}", redisCluster);
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setHostName(redisCluster);
factory.setUsePool(true);
return factory;
}
@Bean
public RedisTemplate<Object, Object> redisTemplate() {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(jedisConnectionFactory());
return redisTemplate;
}
@Bean
public CacheManager cacheManager() {
Map<String, Long> cacheExpiration = new HashMap<>();
cacheExpiration.put(CACHE_RECOMMENDATION, ONE_HOUR);
cacheExpiration.put(CACHE_PICO, ONE_HOUR);
cacheExpiration.put(CACHE_PICO_OUTCOME_C, ONE_HOUR);
cacheExpiration.put(CACHE_PICO_OUTCOME_D, ONE_HOUR);
cacheExpiration.put(CACHE_PICO_OUTCOME_NP, ONE_HOUR);
RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate());
redisCacheManager.setExpires(cacheExpiration);
return redisCacheManager;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment