-
Notifications
You must be signed in to change notification settings - Fork 0
/
SomeValueRedisRepo.java
139 lines (120 loc) · 5.63 KB
/
SomeValueRedisRepo.java
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.lettuce.demo;
import com.lambdaworks.redis.RedisCommandExecutionException;
import com.lambdaworks.redis.ScriptOutputType;
import com.lambdaworks.redis.api.StatefulRedisConnection;
import com.lambdaworks.redis.api.async.RedisAsyncCommands;
import com.lambdaworks.redis.pubsub.RedisPubSubAdapter;
import com.lambdaworks.redis.pubsub.StatefulRedisPubSubConnection;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
@Component
@Slf4j
public class SomeValueRedisRepo {
private static final String EXPIRATION_CHANNEL = "__keyevent@0__:expired";
private static final long TTL_OFFSET = TimeUnit.MINUTES.toSeconds(30);
private static final LuaScript GET_DEL_SCRIPT = new LuaScript(
"local value = redis.call('GET',KEYS[1])" +
"redis.call('DEL',KEYS[1])" +
"return value");
private static final String REDIS_LUA_USE_EVAL = "NOSCRIPT No matching script. Please use EVAL.";
private final RedisAsyncCommands<String, SomeValue> dataCommands;
private final StatefulRedisPubSubConnection<String, String> pubSubConnection;
private final InternalPubSubListener pubSubListener;
private final List<Listener> listeners = new CopyOnWriteArrayList<>();
private ExecutorService executor;
@Autowired
public SomeValueRedisRepo(
StatefulRedisConnection<String, SomeValue> dataConnection,
StatefulRedisPubSubConnection<String, String> pubSubConnection) {
this.dataCommands = dataConnection.async();
this.pubSubConnection = pubSubConnection;
this.pubSubListener = new InternalPubSubListener();
}
@PostConstruct
public void start() {
executor = Executors.newFixedThreadPool(4);
pubSubConnection.sync().addListener(pubSubListener);
pubSubConnection.sync().configSet("notify-keyspace-events", "Eex");
pubSubConnection.sync().subscribe(EXPIRATION_CHANNEL);
}
@PreDestroy
public void stop() {
executor.shutdownNow();
listeners.clear();
pubSubConnection.removeListener(pubSubListener);
pubSubConnection.sync().unsubscribe(EXPIRATION_CHANNEL);
}
public CompletableFuture<String> save(SomeKey someKey, SomeValue value, int ttl) {
CompletableFuture<String> dataFuture = dataCommands.setex(someKey.toDataKey(), ttl + TTL_OFFSET, value).toCompletableFuture();
CompletableFuture<String> expirationFuture = dataCommands.setex(someKey.toExpirationKey(), ttl, null).toCompletableFuture();
return CompletableFuture.allOf(dataFuture, expirationFuture)
.thenApplyAsync(_void -> dataFuture.join(), executor);
}
public CompletableFuture<Optional<SomeValue>> find(SomeKey key) {
return dataCommands.get(key.toDataKey())
.toCompletableFuture()
.thenApplyAsync(Optional::ofNullable, executor);
}
public CompletableFuture<Long> delete(SomeKey key) {
CompletableFuture<Long> dataFuture = dataCommands.del(key.toDataKey()).toCompletableFuture();
CompletableFuture<Long> expirationFuture = dataCommands.del(key.toExpirationKey()).toCompletableFuture();
return CompletableFuture.allOf(dataFuture, expirationFuture)
.thenApplyAsync(_void -> {
return dataFuture.join();
}, executor);
}
public CompletableFuture<Optional<SomeValue>> findAndDeleteAtomically(SomeKey key) {
CompletableFuture<Object> shaFuture = dataCommands.evalsha(
GET_DEL_SCRIPT.getSha1(), ScriptOutputType.VALUE, key.toDataKey()).toCompletableFuture();
return shaFuture
.handleAsync((result, error) -> error, executor)
.thenCompose(error -> {
if (error != null && (error instanceof RedisCommandExecutionException)
&& REDIS_LUA_USE_EVAL.equals(error.getMessage())) {
return dataCommands.eval(GET_DEL_SCRIPT.getScript(), ScriptOutputType.VALUE, key.toDataKey());
}
return shaFuture;
}).thenApply(result -> {
if (result instanceof SomeValue) {
return Optional.of((SomeValue) result);
} else {
return Optional.empty();
}
});
}
public void addListener(Listener listener) {
listeners.add(listener);
}
private class InternalPubSubListener extends RedisPubSubAdapter<String, String> {
@Override
public void message(String channel, String redisKey) {
if (!EXPIRATION_CHANNEL.equals(channel)) {
return;
}
if (SomeKey.isExpirationKeyCandidate(redisKey)) {
executor.execute(() -> {
for (Listener listener : listeners) {
try {
listener.expired(SomeKey.fromExpirationCandidate(redisKey));
} catch (Exception e) {
log.error("Error notifying {}", redisKey, e);
}
}
});
}
}
}
public interface Listener {
void expired(SomeKey key);
}
}