LayeringCacheAutoFactory.java 4.12 KB
package com.irrigation.icl.cache;

import com.irrigation.icl.cache.core.LayeringCacheLoadCondition;
import com.irrigation.icl.cache.core.LayeringCacheManager;
import com.irrigation.icl.cache.core.LayeringCacheProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

import java.time.Duration;

/**
 * @Author: boni
 * @Date: 2018/8/24-下午2:37
 */
@Configuration
@Conditional(LayeringCacheLoadCondition.class)
@EnableConfigurationProperties(LayeringCacheProperties.class)
@Slf4j
public class LayeringCacheAutoFactory {
    @Bean
    public LayeringCacheManager cacheManager(RedisTemplate<Object, Object> redisTemplate,
                                             LayeringCacheProperties layeringCacheProperties) {

        RedisCacheWriter redisCacheWriter = RedisCacheWriter
                .nonLockingRedisCacheWriter(redisTemplate.getConnectionFactory());
        long expire = layeringCacheProperties.getExpire() > 0L ? layeringCacheProperties.getExpire() : 1800L;
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration
                .defaultCacheConfig()
                .entryTtl(Duration.ofSeconds(expire));
        LayeringCacheManager cacheManager =
                new LayeringCacheManager(
                        redisTemplate,
                        redisCacheWriter,
                        redisCacheConfiguration,
                        layeringCacheProperties);
        return cacheManager;
    }

    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                            LayeringCacheProperties layeringCacheProperties,
                                            MessageListenerAdapter listenerAdapter) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        LayeringCacheProperties.L1 l1 = layeringCacheProperties.getCacheL1();
        if (l1 != null) {
            container.addMessageListener(listenerAdapter, new PatternTopic(layeringCacheProperties.getCacheL1().getTopic()));
        }
        return container;
    }

    @Bean
    LayeringCacheSyncCommandHandler layeringCacheSyncCommandHandler(CacheManager cacheManager) {
        LayeringCacheSyncCommandHandler handler = new LayeringCacheSyncCommandHandler(cacheManager);
        return handler;
    }

    @Bean
    MessageListenerAdapter listenerAdapter(LayeringCacheSyncCommandHandler handler,
                                           RedisTemplate<Object, Object> redisTemplate) {

        return new MessageListenerAdapter(new MessageListener() {
            @Override
            public void onMessage(Message message, byte[] pattern) {
                byte[] content = message.getBody();
                if (content != null) {
//                    String commandString = new String(content, Charset.defaultCharset());
//                    Command command = JSON.parseObject(commandString, Command.class);
                    Command command = (Command) redisTemplate.getValueSerializer().deserialize(content);
                    if (command != null) {
                        log.debug(command.toString());
                        handler.handle(command);
                    }
                }
            }

        });
    }
}