LayeringCacheAutoFactory.java
3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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<Object, Object> 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<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);
}
}
}
});
}
}