AS AgentScopeAPI 手册 / API Reference

RedisSession

public class RedisSession implements Session
源码Source io/agentscope/core/session/redis/RedisSession.java:179–483

Redis 会话,在多次调用之间保存和恢复会话与记忆数据。位于 io.agentscope.core.session.redis 包,实现 Session。核心成员包括 builder、save、get、getList、exists、delete。

Redis-based session implementation supporting multiple Redis clients.

This implementation provides a unified interface for Redis-based session storage, supporting multiple Redis client implementations:

  • Jedis - Standalone, Cluster, Sentinel

  • Lettuce - Standalone, Cluster, Sentinel

  • Redisson - Standalone, Cluster, Sentinel, Master/Slave

The session state is stored in Redis with following key structure:

  • Single state: {prefix}{sessionId}:{stateKey} - Redis String containing JSON

  • List state: {prefix}{sessionId}:{stateKey}:list - Redis List containing JSON items

  • List hash: {prefix}{sessionId}:{stateKey}:list:_hash - Hash for change detection

  • Session marker: {prefix}{sessionId}:_keys - Redis Set tracking all state keys

Jedis Usage Examples:

Jedis Standalone (using RedisClient):

`// 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):

`// 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):

`// 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:

`// Create Lettuce RedisClient
RedisClient redisClient = RedisClient.create("redis://localhost:6379");

// Build RedisSession
Session session = RedisSession.builder()
.lettuceClient(redisClient)
.build();
`

Lettuce Cluster:

`// Create Lettuce RedisClusterClient for cluster mode
RedisClusterClient clusterClient = RedisClusterClient.create(
RedisURI.create("localhost", 7000));

// Build RedisSession
Session session = RedisSession.builder()
.lettuceClusterClient(clusterClient)
.build();
`

Lettuce Sentinel:

`// 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:

`// 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();
`

Custom Key Prefix Example:

`// Create Redis client
RedisClient redisClient = RedisClient.create("redis://localhost:6379");

// Build RedisSession with custom key prefix
Session session = RedisSession.builder()
.jedisClient(redisClient)
.keyPrefix("myapp:session:")
.build();
`

嵌套类摘要Nested Class Summary

类Class类型Kind说明Description
Builder类class
`RedisSession`的构建器。
Builder for `RedisSession`.

方法摘要Method Summary

方法Method说明Description
public static Builder builder()
static
创建新的构建器用于`RedisSession`。
Creates a new builder for `RedisSession`.
public void save(SessionKey sessionKey, String key, State value)
保存数据到持久化存储。
Performs the save operation.
public void save(SessionKey sessionKey, String key, List<? extends State> values)
保存数据到持久化存储。
Performs the save operation.
public T extends State Optional<T> get(SessionKey sessionKey, String key, Class<T> type)
按条件读取数据。
Performs the get operation.
public T extends State List<T> getList(SessionKey sessionKey, String key, Class<T> itemType)
获取列表。
Returns the list.
public boolean exists(SessionKey sessionKey)
判断数据是否存在。
Performs the exists operation.
public void delete(SessionKey sessionKey)
删除数据。
Performs the delete operation.
public Set<SessionKey> listSessionKeys()
列出会话密钥。
Lists the session keys.
public void close()
关闭连接并释放相关资源。
Releases resources and connections.
public Mono<Integer> clearAllSessions()
清空所有会话。
Clear all sessions stored in Redis (for testing or cleanup).

方法详情Method Detail

builder

public static Builder builder()
static

创建新的构建器用于RedisSession。

Creates a new builder for RedisSession.

返回Returns Builder
一种新的构建器实例。a new Builder instance
源码Source io/agentscope/core/session/redis/RedisSession.java:209–211

save

public void save(SessionKey sessionKey, String key, State value)

保存数据到持久化存储。

Performs the save operation.

参数Parameters
名称Name类型Type说明Description
sessionKeySessionKey会话键(类型 `SessionKey`)。The session key.
keyString键(类型 `String`)。The key.
valueState值(类型 `State`)。The value.
源码Source io/agentscope/core/session/redis/RedisSession.java:213–226

save

public void save(SessionKey sessionKey, String key, List<? extends State> values)

保存数据到持久化存储。

Performs the save operation.

参数Parameters
名称Name类型Type说明Description
sessionKeySessionKey会话键(类型 `SessionKey`)。The session key.
keyString键(类型 `String`)。The key.
valuesList<? extends State>值(类型 `List`)。The values.
源码Source io/agentscope/core/session/redis/RedisSession.java:228–267

get

public T extends State Optional<T> get(SessionKey sessionKey, String key, Class<T> type)

按条件读取数据。

Performs the get operation.

参数Parameters
名称Name类型Type说明Description
sessionKeySessionKey会话键(类型 `SessionKey`)。The session key.
keyString键(类型 `String`)。The key.
typeClass<T>类型(类型 `Class`)。The type.
返回Returns Optional<T>
返回可选。Returns optional.
源码Source io/agentscope/core/session/redis/RedisSession.java:269–282

getList

public T extends State List<T> getList(SessionKey sessionKey, String key, Class<T> itemType)

获取列表。

Returns the list.

参数Parameters
名称Name类型Type说明Description
sessionKeySessionKey会话键(类型 `SessionKey`)。The session key.
keyString键(类型 `String`)。The key.
itemTypeClass<T>条目类型(类型 `Class`)。The item type.
返回Returns List<T>
返回列出。Returns list.
源码Source io/agentscope/core/session/redis/RedisSession.java:284–302

exists

public boolean exists(SessionKey sessionKey)

判断数据是否存在。

Performs the exists operation.

参数Parameters
名称Name类型Type说明Description
sessionKeySessionKey会话键(类型 `SessionKey`)。The session key.
返回Returns boolean
返回布尔值,表示判断结果。Returns a boolean result.
源码Source io/agentscope/core/session/redis/RedisSession.java:304–315

delete

public void delete(SessionKey sessionKey)

删除数据。

Performs the delete operation.

参数Parameters
名称Name类型Type说明Description
sessionKeySessionKey会话键(类型 `SessionKey`)。The session key.
源码Source io/agentscope/core/session/redis/RedisSession.java:317–349

listSessionKeys

public Set<SessionKey> listSessionKeys()

列出会话密钥。

Lists the session keys.

返回Returns Set<SessionKey>
返回设置。Returns set.
源码Source io/agentscope/core/session/redis/RedisSession.java:351–369

close

public void close()

关闭连接并释放相关资源。

Releases resources and connections.

源码Source io/agentscope/core/session/redis/RedisSession.java:371–374

clearAllSessions

public Mono<Integer> clearAllSessions()

清空所有会话。

Clear all sessions stored in Redis (for testing or cleanup).

返回Returns Mono<Integer>
返回Mono。Mono that completes with the number of deleted session keys
源码Source io/agentscope/core/session/redis/RedisSession.java:381–395

嵌套类详情Nested Class Detail

Builder

public static class Builder

RedisSession的构建器。位于 io.agentscope.core.session.redis 包。核心成员包括 keyPrefix、jedisClient、lettuceClient、lettuceClusterClient、redissonClient、clientAdapter。

Builder for RedisSession.

The builder supports multiple Redis client types. Only one client type should be set.

Supported client types:

  • Jedis: #jedisClient(UnifiedJedis)

  • Lettuce Standalone/Sentinel: #lettuceClient(RedisClient)

  • Lettuce Cluster: #lettuceClusterClient(RedisClusterClient)

  • Redisson: #redissonClient(RedissonClient)

  • Custom: #clientAdapter(RedisClientAdapter)

继承成员Inherited Members

继承自Methods inherited from Session
基于官方固定版本源码静态生成Generated from pinned official sources · Python 2.0.11 · Java 2.0.4
签名与文档为静态抽取;如有疑问请以源码为准。Signatures and docs are extracted statically; verify against the source when in doubt.