All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.bootfastconfig.cache.L2CacheConfig Maven / Gradle / Ivy

Go to download

Parent pom providing dependency and plugin management for applications built with Maven

The newest version!
package com.github.bootfastconfig.cache;

import com.github.bootfastconfig.cache.properties.L2CacheProperties;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

/**
 * @author mister
 */
@ConditionalOnClass(value = {CaffeineCacheManager.class, RedisCacheManager.class})
@ConditionalOnBean(value = {RedisCacheManager.class, CaffeineCacheManager.class, RedisTemplate.class})
public class L2CacheConfig implements InitializingBean {


    @Autowired
    private L2CacheProperties l2Cache;


    @Autowired
    private ConfigurableApplicationContext applicationContext;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedisCaffeineL2CacheManager redisCaffeineL2CacheManager;

    @Autowired
    private RedisMessageListenerContainer redisMessageListenerContainer;


    @Bean(CacheBeanNameConstant.L2_CACHE_MANAGER)
    public RedisCaffeineL2CacheManager cacheManager(CaffeineCacheManager caffeineCacheManager, RedisCacheManager redisCacheManager, RedisTemplate redisTemplate) {
        return new RedisCaffeineL2CacheManager(l2Cache, redisTemplate, caffeineCacheManager, redisCacheManager);
    }

    @Bean
    @ConditionalOnBean(RedisCaffeineL2CacheManager.class)
    @ConditionalOnMissingBean(RedisMessageListenerContainer.class)
    public RedisMessageListenerContainer redisMessageListenerContainer(RedisTemplate redisTemplate) {
        RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
        redisMessageListenerContainer.setConnectionFactory(redisTemplate.getConnectionFactory());
        return redisMessageListenerContainer;
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        if (redisMessageListenerContainer != null) {
            CacheMessageRedisListener cacheMessageListener = new CacheMessageRedisListener(redisTemplate, redisCaffeineL2CacheManager);
            redisMessageListenerContainer.addMessageListener(cacheMessageListener, new ChannelTopic(l2Cache.getTopic()));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy