Skip to content

Instantly share code, notes, and snippets.

@arleighdickerson
Created July 13, 2025 16:54
Show Gist options
  • Save arleighdickerson/daed426458879dddec849649d8bb0410 to your computer and use it in GitHub Desktop.
Save arleighdickerson/daed426458879dddec849649d8bb0410 to your computer and use it in GitHub Desktop.
import io.arleigh.gantry.cache.PrefixedKeyGenerator;
import io.arleigh.gantry.properties.AppRedisProperties;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.val;
import org.redisson.api.RedissonClient;
import org.redisson.jcache.configuration.RedissonConfiguration;
import org.redisson.spring.cache.RedissonSpringCacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizer;
import org.springframework.boot.info.BuildProperties;
import org.springframework.boot.info.GitProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.AdviceMode;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.UserCache;
import org.springframework.security.core.userdetails.cache.SpringCacheBasedUserCache;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.expiry.CreatedExpiryPolicy;
import javax.cache.expiry.Duration;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
@Configuration
@RequiredArgsConstructor
@EnableCaching(mode = AdviceMode.ASPECTJ, proxyTargetClass = true)
class CacheConfiguration extends CachingConfigurerSupport implements ApplicationContextAware {
private static final String SPRING_CACHE_NAME = "users";
@Setter(onMethod_ = @Autowired)
private GitProperties gitProperties;
@Setter(onMethod_ = @Autowired)
private BuildProperties buildProperties;
@Setter(onMethod_ = @Autowired)
private RedissonClient redissonClient;
@Setter
private ApplicationContext applicationContext;
@Bean
public UserCache userCache() {
return new SpringCacheBasedUserCache(Objects.requireNonNull(cacheManager()).getCache(SPRING_CACHE_NAME));
}
@Bean
public javax.cache.configuration.Configuration<Object, Object> jcacheConfiguration(RedissonClient redisson, AppRedisProperties appRedisProperties) {
MutableConfiguration<Object, Object> jcacheConfig = new MutableConfiguration<>();
jcacheConfig.setStatisticsEnabled(true);
jcacheConfig.setExpiryPolicyFactory(
CreatedExpiryPolicy.factoryOf(
new Duration(TimeUnit.SECONDS, appRedisProperties.getExpiration())
)
);
return RedissonConfiguration.fromInstance(redisson, jcacheConfig);
}
@Bean
public JCacheManagerCustomizer cacheManagerCustomizer
(javax.cache.configuration.Configuration<Object, Object> jcacheConfiguration) {
return cacheManager -> {
createCache(cacheManager, "buckets", jcacheConfiguration);
};
}
private void createCache(
javax.cache.CacheManager cacheManager,
@SuppressWarnings("SameParameterValue") String cacheName,
javax.cache.configuration.Configuration<Object, Object> jcacheConfiguration
) {
javax.cache.Cache<Object, Object> cache = cacheManager.getCache(cacheName);
if (cache != null) {
cache.clear();
} else {
cacheManager.createCache(cacheName, jcacheConfiguration);
}
}
@Override
@Bean
public KeyGenerator keyGenerator() {
return new PrefixedKeyGenerator(gitProperties, buildProperties);
}
@Override
@Bean
public CacheManager cacheManager() {
val cacheManager = new RedissonSpringCacheManager(redissonClient);
cacheManager.setTransactionAware(true);
cacheManager.setCacheNames(List.of(SPRING_CACHE_NAME));
cacheManager.setResourceLoader(Objects.requireNonNull(applicationContext));
return cacheManager;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment