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.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; /** * @Author: boni * @Date: 2018/8/24-下午2:37 */ @Configuration @Conditional(LayeringCacheLoadCondition.class) @EnableConfigurationProperties(LayeringCacheProperties.class) @Slf4j public class LayeringCacheAutoFactory { @Bean public LayeringCacheManager cacheManager(RedisTemplate redisTemplate, LayeringCacheProperties layeringCacheProperties) { LayeringCacheManager cacheManager = new LayeringCacheManager(redisTemplate, layeringCacheProperties); cacheManager.setUsePrefix(true); long expire = layeringCacheProperties.getExpire() > 0L ? layeringCacheProperties.getExpire() : 120L; cacheManager.setExpires(layeringCacheProperties.getExpires()); cacheManager.setDefaultExpiration(expire); return cacheManager; } @Bean RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, LayeringCacheProperties layeringCacheProperties, MessageListenerAdapter listenerAdapter) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); 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 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); } } } }); } }