Skip to content

Commit 29b7c00

Browse files
committed
feat(backend): add redis interface for registering docId -> broadcast server route
1 parent 93f66b6 commit 29b7c00

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

scalable-ot-core/src/main/java/com/brotherjing/Const.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ public interface Const {
99

1010
String REVISION_CONSUMER_GROUP_ID = "revision_consumer";
1111

12+
/**
13+
* Redis
14+
*/
15+
String ROUTE_PREFIX = "route:";
16+
int ROUTE_EXPIRE_MINUTES = 30;
17+
1218
/**
1319
* Zookeeper
1420
*/

scalable-ot-core/src/main/java/com/brotherjing/config/RedisConfig.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
import org.springframework.context.annotation.Configuration;
55
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
66
import org.springframework.data.redis.core.RedisTemplate;
7+
import org.springframework.data.redis.core.ValueOperations;
78
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
9+
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
10+
import org.springframework.data.redis.serializer.StringRedisSerializer;
11+
12+
import com.brotherjing.core.loadbalance.ServerEntity;
813

914
@Configuration
1015
@EnableRedisRepositories
@@ -15,9 +20,19 @@ JedisConnectionFactory jedisConnectionFactory() {
1520
}
1621

1722
@Bean
18-
public RedisTemplate<String, Object> redisTemplate() {
19-
RedisTemplate<String, Object> template = new RedisTemplate<>();
23+
public RedisTemplate<String, ServerEntity> routeTemplate() {
24+
RedisTemplate<String, ServerEntity> template = new RedisTemplate<>();
2025
template.setConnectionFactory(jedisConnectionFactory());
26+
27+
template.setKeySerializer(new StringRedisSerializer());
28+
template.setValueSerializer(new Jackson2JsonRedisSerializer<>(ServerEntity.class));
29+
template.afterPropertiesSet();
30+
2131
return template;
2232
}
33+
34+
@Bean
35+
public ValueOperations<String, ServerEntity> routeValueOperations() {
36+
return routeTemplate().opsForValue();
37+
}
2338
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.brotherjing.core.dao;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.data.redis.core.RedisTemplate;
7+
import org.springframework.data.redis.core.ValueOperations;
8+
import org.springframework.stereotype.Repository;
9+
10+
import com.brotherjing.Const;
11+
import com.brotherjing.core.loadbalance.ServerEntity;
12+
13+
@Repository
14+
public class RedisDao {
15+
16+
@Autowired
17+
private RedisTemplate<String, ServerEntity> routeTemplate;
18+
19+
@Autowired
20+
private ValueOperations<String, ServerEntity> routeValueOperations;
21+
22+
public void addRoute(String docId, ServerEntity server) {
23+
routeValueOperations.set(getRouteKey(docId), server, Const.ROUTE_EXPIRE_MINUTES, TimeUnit.MINUTES);
24+
}
25+
26+
public ServerEntity getRoute(String docId) {
27+
return routeValueOperations.get(docId);
28+
}
29+
30+
public void removeRoute(String docId) {
31+
routeTemplate.delete(getRouteKey(docId));
32+
}
33+
34+
private String getRouteKey(String docId) {
35+
return Const.ROUTE_PREFIX + docId;
36+
}
37+
}

0 commit comments

Comments
 (0)