-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathRedisConfig.java
More file actions
72 lines (62 loc) · 3.19 KB
/
RedisConfig.java
File metadata and controls
72 lines (62 loc) · 3.19 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
package com.example.solidconnection.common.config.redis;
import static com.example.solidconnection.redis.RedisConstants.CREATE_CHANNEL;
import com.example.solidconnection.cache.CacheUpdateListener;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
@EnableRedisRepositories
public class RedisConfig {
private final String redisHost;
private final int redisPort;
public RedisConfig(@Value("${spring.data.redis.host}") final String redisHost,
@Value("${spring.data.redis.port}") final int redisPort) {
this.redisHost = redisHost;
this.redisPort = redisPort;
}
@Bean
public RedisConnectionFactory redisConnectionFactory() {
return new LettuceConnectionFactory(redisHost, redisPort);
}
@Bean
public RedisTemplate<String, String> redisTemplate() {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(redisConnectionFactory());
return redisTemplate;
}
@Bean
public RedisTemplate<String, Object> objectRedisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(new ObjectMapper(), Object.class));
redisTemplate.setConnectionFactory(redisConnectionFactory());
return redisTemplate;
}
@Bean
RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory,
CacheUpdateListener listener) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.addMessageListener(listener, new PatternTopic(CREATE_CHANNEL.getValue()));
return container;
}
@Bean(name = "incrViewCountScript")
public RedisScript<Long> incrViewCountLuaScript() {
Resource scriptSource = new ClassPathResource("scripts/incrViewCount.lua");
return RedisScript.of(scriptSource, Long.class);
}
}