Class LettuceClientAdapter
java.lang.Object
io.agentscope.extensions.redis.state.lettuce.LettuceClientAdapter
- All Implemented Interfaces:
RedisClientAdapter
Adapter for Lettuce Redis client.
This adapter supports multiple Redis deployment modes through different client types:
RedisClient- Standalone and Sentinel modesRedisClusterClient- Cluster mode
The adapter internally manages a shared connection and commands instance for efficient connection usage.
This implementation uses direct conditional checks to handle Lettuce's separate command APIs for standalone/sentinel and cluster modes. This is the simplest approach given Lettuce's lack of a unified API across deployment modes.
Usage Examples:
Standalone Mode:
// Create standalone RedisClient
RedisClient redisClient = RedisClient.create("redis://localhost:6379");
// Use with RedisAgentStateStore
AgentStateStore stateStore = RedisAgentStateStore.builder()
.lettuceClient(redisClient)
.build();
Cluster Mode:
// Create cluster client
RedisClusterClient clusterClient = RedisClusterClient.create("redis://localhost:7000");
// Use with RedisAgentStateStore
AgentStateStore stateStore = RedisAgentStateStore.builder()
.lettuceClusterClient(clusterClient)
.build();
Sentinel Mode:
// Create Lettuce RedisClient for sentinel
RedisURI sentinelUri = RedisURI.builder()
.withSentinelMasterId("mymaster")
.withSentinel("localhost", 26379)
.withSentinel("localhost", 26380)
.withSentinel("localhost", 26381)
.withDatabase(0)
.build();
RedisClient redisClient = RedisClient.create(sentinelUri);
// Use with RedisAgentStateStore
AgentStateStore stateStore = RedisAgentStateStore.builder()
.lettuceClient(redisClient)
.build();
-
Method Summary
Modifier and TypeMethodDescriptionvoidAdd a member to a set.voidclose()Close the adapter and release resources.voiddeleteKeys(String... keys) Delete one or more keys.longExecute a Lua script atomically.findKeysByPattern(String pattern) Find all keys matching a pattern.Get a string value.longgetListLength(String key) Get the length of a list.getSetMembers(String key) Get all members of a set.longgetSetSize(String key) Get the number of members in a set.booleanCheck if a key exists.static LettuceClientAdapterof(io.lettuce.core.cluster.RedisClusterClient redisClusterClient) Create adapter from RedisClusterClient (cluster mode).static LettuceClientAdapterof(io.lettuce.core.RedisClient redisClient) Create adapter from RedisClient (standalone/sentinel mode).Get a range of elements from a list.voidrightPushList(String key, String value) Append a value to the right end of a list.voidSet a string value.
-
Method Details
-
of
Create adapter from RedisClient (standalone/sentinel mode).- Parameters:
redisClient- the RedisClient for standalone or sentinel mode- Returns:
- a new LettuceClientAdapter
- Throws:
NullPointerException- if redisClient is null
-
of
public static LettuceClientAdapter of(io.lettuce.core.cluster.RedisClusterClient redisClusterClient) Create adapter from RedisClusterClient (cluster mode).- Parameters:
redisClusterClient- the RedisClusterClient for cluster mode- Returns:
- a new LettuceClientAdapter
- Throws:
NullPointerException- if redisClusterClient is null
-
set
Description copied from interface:RedisClientAdapterSet a string value.- Specified by:
setin interfaceRedisClientAdapter- Parameters:
key- the Redis keyvalue- the string value
-
get
Description copied from interface:RedisClientAdapterGet a string value.- Specified by:
getin interfaceRedisClientAdapter- Parameters:
key- the Redis key- Returns:
- the string value, or null if not found
-
rightPushList
Description copied from interface:RedisClientAdapterAppend a value to the right end of a list.- Specified by:
rightPushListin interfaceRedisClientAdapter- Parameters:
key- the Redis list keyvalue- the value to append
-
rangeList
Description copied from interface:RedisClientAdapterGet a range of elements from a list.- Specified by:
rangeListin interfaceRedisClientAdapter- Parameters:
key- the Redis list keystart- the start index (inclusive)end- the end index (inclusive, -1 for all elements)- Returns:
- list of values
-
getListLength
Description copied from interface:RedisClientAdapterGet the length of a list.- Specified by:
getListLengthin interfaceRedisClientAdapter- Parameters:
key- the Redis list key- Returns:
- the length of the list
-
deleteKeys
Description copied from interface:RedisClientAdapterDelete one or more keys.- Specified by:
deleteKeysin interfaceRedisClientAdapter- Parameters:
keys- the keys to delete
-
addToSet
Description copied from interface:RedisClientAdapterAdd a member to a set.- Specified by:
addToSetin interfaceRedisClientAdapter- Parameters:
key- the Redis set keymember- the member to add
-
getSetMembers
Description copied from interface:RedisClientAdapterGet all members of a set.- Specified by:
getSetMembersin interfaceRedisClientAdapter- Parameters:
key- the Redis set key- Returns:
- set of members
-
getSetSize
Description copied from interface:RedisClientAdapterGet the number of members in a set.- Specified by:
getSetSizein interfaceRedisClientAdapter- Parameters:
key- the Redis set key- Returns:
- the number of members
-
keyExists
Description copied from interface:RedisClientAdapterCheck if a key exists.- Specified by:
keyExistsin interfaceRedisClientAdapter- Parameters:
key- the Redis key- Returns:
- true if the key exists
-
findKeysByPattern
Description copied from interface:RedisClientAdapterFind all keys matching a pattern.- Specified by:
findKeysByPatternin interfaceRedisClientAdapter- Parameters:
pattern- the key pattern (e.g., "prefix:*")- Returns:
- set of matching keys
-
evalScript
Description copied from interface:RedisClientAdapterExecute a Lua script atomically.- Specified by:
evalScriptin interfaceRedisClientAdapter- Parameters:
script- the Lua script sourcekeys- Redis keys passed asKEYSargs- Redis arguments passed asARGV- Returns:
- the script return value as a long
-
close
public void close()Description copied from interface:RedisClientAdapterClose the adapter and release resources.- Specified by:
closein interfaceRedisClientAdapter
-