Commit 09b9780
authored
feat(session): a unified Redis session client that supports switching between underlying clients (#611)
## AgentScope-Java Version
1.0.8-SNAPSHOT
## Description
releted to #610
### Background
The current JedisSeesion uses JedisPool, which only supports standalone
scenarios. In production environments, users mostly use more complex
deployment methods, such as sentinels, clusters, and master-slave
setups. Furthermore, in a Java environment, users may use different
Redis client implementations, such as `Jedis`, `Lettuce`, and
`Redisson`. If each is used independently, scaling up with different
Redis client implementations would result in `a lot of duplicate code`.
Moreover, naming conventions like JedisSeesion are unclear, as they are
all essentially types of Redis clients.
### New Feature
- **Unified the RedisSession client**: using the adapter pattern to
implement different underlying Redis clients, and consolidated the
duplicate code that was originally scattered across various client
implementations into the RedisSession.
- **Various Redis deployment modes**: Jedis, Lettuce, Redisson adopt
more modern APIs and support various deployment modes such as
standalone, sentinel, cluster, and master-slave.
- **README**: Add a README introduction for the
`agentscope-extensions-session-redis` module
The newly added codes all have complete javadoc documentation and the
`mvn test` has passed.
### Modify
- Added lettuce dependency
- Add the "deprecated" tag to the old versions of JedisSession and
RedissonSession, and update the corresponding javadoc.
### New Usage
#### Jedis Usage Examples
- Jedis Standalone (using RedisClient):
```java
// Create Jedis RedisClient (new API)
RedisClient redisClient = RedisClient.create("redis://localhost:6379");
// Build RedisSession
Session session = RedisSession.builder()
.jedisClient(redisClient)
.build();
```
- Jedis Cluster (using RedisClusterClient)
```java
// Create Jedis RedisClusterClient
Set<HostAndPort> nodes = new HashSet<>();
nodes.add(new HostAndPort("localhost", 7000));
nodes.add(new HostAndPort("localhost", 7001));
nodes.add(new HostAndPort("localhost", 7002));
RedisClusterClient redisClusterClient = RedisClusterClient.create(nodes);
// Build RedisSession
Session session = RedisSession.builder()
.jedisClient(redisClusterClient)
.build();
```
- Jedis Sentinel (using RedisSentinelClient)
```java
// Create Jedis RedisSentinelClient
Set<String> sentinelNodes = new HashSet<>();
sentinelNodes.add("localhost:26379");
sentinelNodes.add("localhost:26380");
RedisSentinelClient redisSentinelClient = RedisSentinelClient.create("mymaster", sentinelNodes);
// Build RedisSession
Session session = RedisSession.builder()
.jedisClient(redisSentinelClient)
.build();
```
#### Lettuce Usage Examples
- Lettuce Standalone
```java
// Create Lettuce RedisClient
RedisClient redisClient = RedisClient.create("redis://localhost:6379");
// Build RedisSession
Session session = RedisSession.builder()
.lettuceClient(redisClient)
.build();
```
- Lettuce Cluster
```java
// Create Lettuce RedisClient for cluster
RedisURI clusterUri = RedisURI.create("redis://localhost:7000");
RedisClient redisClient = RedisClient.create(clusterUri);
// Build RedisSession
Session session = RedisSession.builder()
.lettuceClient(redisClient)
.build();
```
- Lettuce Sentinel
```java
// Create Lettuce RedisClient for sentinel
RedisURI sentinelUri = RedisURI.builder()
.withSentinelMasterId("mymaster")
.withSentinel("localhost", 26379)
.withSentinel("localhost", 26380)
.build();
RedisClient redisClient = RedisClient.create(sentinelUri);
// Build RedisSession
Session session = RedisSession.builder()
.lettuceClient(redisClient)
.build();
```
#### Redisson Usage Example
```java
// Create RedissonClient (configure as needed for your deployment mode)
Config config = new Config();
config.useSingleServer().setAddress("redis://localhost:6379");
// or for cluster: config.useClusterServers().addNodeAddress("redis://localhost:7000");
// or for sentinel: config.useSentinelServers().setMasterName("mymaster").addSentinelAddress("redis://localhost:26379");
RedissonClient redissonClient = Redisson.create(config);
// Build RedisSession
Session session = RedisSession.builder()
.redissonClient(redissonClient)
.build();
```
## Checklist
Please check the following items before code is ready to be reviewed.
- [x] Code has been formatted with `mvn spotless:apply`
- [x] All tests are passing (`mvn test`)
- [x] Javadoc comments are complete and follow project conventions
- [x] Related documentation has been updated (e.g. links, examples,
etc.)
- [x] Code is ready for review1 parent a163540 commit 09b9780
14 files changed
Lines changed: 2473 additions & 135 deletions
File tree
- agentscope-dependencies-bom
- agentscope-extensions/agentscope-extensions-session-redis
- src
- main/java/io/agentscope/core/session/redis
- jedis
- lettuce
- redisson
- test/java/io/agentscope/core/session/redis
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
99 | 99 | | |
100 | 100 | | |
101 | 101 | | |
| 102 | + | |
102 | 103 | | |
103 | 104 | | |
104 | 105 | | |
| |||
378 | 379 | | |
379 | 380 | | |
380 | 381 | | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
381 | 389 | | |
382 | 390 | | |
383 | 391 | | |
| |||
Lines changed: 244 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
0 commit comments