Skip to content

Commit ebf3d9d

Browse files
committed
Add data version mechanism in local ConfigCache
Signed-off-by: Eric Zhao <sczyh16@gmail.com>
1 parent 8c19164 commit ebf3d9d

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

src/main/java/io/opensergo/subscribe/SubscribedConfigCache.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,40 @@
1616
package io.opensergo.subscribe;
1717

1818
import java.util.List;
19+
import java.util.Optional;
1920
import java.util.concurrent.ConcurrentHashMap;
2021
import java.util.concurrent.ConcurrentMap;
2122

2223
/**
2324
* @author Eric Zhao
25+
* @author xzd
2426
*/
2527
public class SubscribedConfigCache {
2628

27-
private final ConcurrentMap<SubscribeKey, List<Object>> dataMap = new ConcurrentHashMap<>();
29+
private final ConcurrentMap<SubscribeKey, SubscribedData> dataMap = new ConcurrentHashMap<>();
2830

29-
public void updateData(SubscribeKey key, List<Object> data) {
30-
dataMap.put(key, data);
31+
public void updateData(SubscribeKey key, Object data, long version) {
32+
// TODO: guarantee the latest version
33+
dataMap.put(key, new SubscribedData(version, data));
3134
}
3235

33-
public List<Object> getDataFor(SubscribeKey key) {
36+
public SubscribedData getDataFor(SubscribeKey key) {
3437
if (key == null) {
3538
return null;
3639
}
3740
return dataMap.get(key);
3841
}
42+
43+
public Optional<Long> getDataVersionFor(SubscribeKey key) {
44+
SubscribedData d = getDataFor(key);
45+
return Optional.ofNullable(getDataFor(key)).map(SubscribedData::getVersion);
46+
}
47+
48+
public <T> List<T> getDataListFor(SubscribeKey key, Class<T> clazz) {
49+
SubscribedData d = getDataFor(key);
50+
if (d == null || d.getData() == null) {
51+
return null;
52+
}
53+
return (List<T>) d.getData();
54+
}
3955
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2022, OpenSergo Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.opensergo.subscribe;
17+
18+
/**
19+
* @author Eric Zhao
20+
*/
21+
public class SubscribedData {
22+
23+
private long version;
24+
private Object data;
25+
26+
public SubscribedData() {}
27+
28+
public SubscribedData(long version, Object data) {
29+
this.version = version;
30+
this.data = data;
31+
}
32+
33+
public long getVersion() {
34+
return version;
35+
}
36+
37+
public SubscribedData setVersion(long version) {
38+
this.version = version;
39+
return this;
40+
}
41+
42+
public Object getData() {
43+
return data;
44+
}
45+
46+
public SubscribedData setData(Object data) {
47+
this.data = data;
48+
return this;
49+
}
50+
51+
@Override
52+
public String toString() {
53+
return "SubscribedData{" +
54+
"version='" + version + '\'' +
55+
", data=" + data +
56+
'}';
57+
}
58+
}

0 commit comments

Comments
 (0)