RedisStore
public class RedisStore implements BaseStore
io/agentscope/extensions/redis/store/RedisStore.java:57–283Redis 存储,负责持久化与读取数据。位于 io.agentscope.extensions.redis.store 包,实现 BaseStore。核心成员包括 get、put、putIfVersion、search、delete、RedisStore。
Redis-backed BaseStore implementation.
Key layout
For each item with namespace [a, b, c] and key k, two Redis keys are used:
-
Item hash
<prefix>item:<ns>\0<k>— a Redis hash with fieldsvalue(JSON-encodedMap<String,Object>) andversion(a stringified long). -
Namespace index
<prefix>idx:<ns>— a sorted set (all scores0) holding everykwritten under that exact namespace, enabling lexicographic#searchviaZRANGEBYLEXwithout scanning the keyspace.
<ns> is the namespace components joined with "\0".
Concurrency
#put and #putIfVersion both run as a single Lua EVAL, making the
version read + hash write + index update one atomic Redis operation. This makes
#putIfVersion safe to use as a distributed compare-and-swap primitive across multiple
processes sharing the same Redis instance.
#search is not in the same transaction as #put: index members may
temporarily refer to an item whose hash has not yet been written (window between ZADD
and HSET, which is closed by the Lua atomicity above) or to an item that was
concurrently deleted. The implementation tolerates the latter by skipping missing items.
字段摘要Field Summary
| 字段Field | 类型Type | 说明Description |
|---|---|---|
DEFAULT_KEY_PREFIXstaticfinal | String | 默认键 PREFIX字段。 Default Redis key prefix; pick something distinctive enough not to collide with other apps. |
构造器摘要Constructor Summary
| 构造器Constructor | 说明Description |
|---|---|
public RedisStore(UnifiedJedis jedis) | 初始化 `RedisStore` 实例。 Creates a Redis-backed store with the default key prefix (`#DEFAULT_KEY_PREFIX`). |
public RedisStore(UnifiedJedis jedis, String keyPrefix) | 初始化 `RedisStore` 实例。 Creates a Redis-backed store. |
public RedisStore(UnifiedJedis jedis, String keyPrefix, ObjectMapper objectMapper) | 初始化 `RedisStore` 实例。 Creates a Redis-backed store with a custom JSON mapper. |
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
public StoreItem get(List<String> namespace, String key) | 按条件读取数据。 Performs the get operation. |
public void put(List<String> namespace, String key, Map<String, Object> value) | 执行 `put` 方法。 Performs the put operation. |
public boolean putIfVersion(List<String> namespace, String key, Map<String, Object> value, long expectedVersion) | 执行 `putIfVersion` 方法。 Performs the put if version operation. |
public List<StoreItem> search(List<String> namespace, int limit, int offset) | 执行搜索相关操作。 Performs the search operation. |
public void delete(List<String> namespace, String key) | 删除数据。 Performs the delete operation. |
字段详情Field Detail
DEFAULT_KEY_PREFIX
public static final String DEFAULT_KEY_PREFIX
默认键 PREFIX字段。声明为 public static final String DEFAULT_KEY_PREFIX。
Default Redis key prefix; pick something distinctive enough not to collide with other apps.
io/agentscope/extensions/redis/store/RedisStore.java:60构造器详情Constructor Detail
RedisStore
public RedisStore(UnifiedJedis jedis)
初始化 RedisStore 实例。
Creates a Redis-backed store with the default key prefix (#DEFAULT_KEY_PREFIX).
| 名称Name | 类型Type | 说明Description |
|---|---|---|
jedis | UnifiedJedis | jedis(类型 `UnifiedJedis`)。initialized Jedis client; must not be `null` |
io/agentscope/extensions/redis/store/RedisStore.java:100–102RedisStore
public RedisStore(UnifiedJedis jedis, String keyPrefix)
初始化 RedisStore 实例。
Creates a Redis-backed store.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
jedis | UnifiedJedis | jedis(类型 `UnifiedJedis`)。initialized Jedis client; must not be `null` |
keyPrefix | String | 键 Prefix(类型 `String`)。Redis key prefix; trailing `":"` is added if absent. If blank, the default (`#DEFAULT_KEY_PREFIX`) is used. |
io/agentscope/extensions/redis/store/RedisStore.java:111–113RedisStore
public RedisStore(UnifiedJedis jedis, String keyPrefix, ObjectMapper objectMapper)
初始化 RedisStore 实例。
Creates a Redis-backed store with a custom JSON mapper.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
jedis | UnifiedJedis | jedis(类型 `UnifiedJedis`)。initialized Jedis client; must not be `null` |
keyPrefix | String | 键 Prefix(类型 `String`)。Redis key prefix; trailing `":"` is added if absent |
objectMapper | ObjectMapper | 对象 Mapper(类型 `ObjectMapper`)。Jackson mapper used to serialize / deserialize item values |
io/agentscope/extensions/redis/store/RedisStore.java:122–126方法详情Method Detail
get
public StoreItem get(List<String> namespace, String key)
按条件读取数据。
Performs the get operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
namespace | List<String> | 命名空间(类型 `List`)。The namespace. |
key | String | 键(类型 `String`)。The key. |
StoreItemio/agentscope/extensions/redis/store/RedisStore.java:128–137put
public void put(List<String> namespace, String key, Map<String, Object> value)
执行 put 方法。
Performs the put operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
namespace | List<String> | 命名空间(类型 `List`)。The namespace. |
key | String | 键(类型 `String`)。The key. |
value | Map<String, Object> | 值(类型 `Map`)。The value. |
io/agentscope/extensions/redis/store/RedisStore.java:139–146putIfVersion
public boolean putIfVersion(List<String> namespace, String key, Map<String, Object> value, long expectedVersion)
执行 putIfVersion 方法。
Performs the put if version operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
namespace | List<String> | 命名空间(类型 `List`)。The namespace. |
key | String | 键(类型 `String`)。The key. |
value | Map<String, Object> | 值(类型 `Map`)。The value. |
expectedVersion | long | expected 版本(类型 `long`)。The expected version. |
booleanio/agentscope/extensions/redis/store/RedisStore.java:148–164search
public List<StoreItem> search(List<String> namespace, int limit, int offset)
执行搜索相关操作。
Performs the search operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
namespace | List<String> | 命名空间(类型 `List`)。The namespace. |
limit | int | 数量上限(类型 `int`)。The limit. |
offset | int | 偏移量(类型 `int`)。The offset. |
List<StoreItem>io/agentscope/extensions/redis/store/RedisStore.java:166–186delete
public void delete(List<String> namespace, String key)
删除数据。
Performs the delete operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
namespace | List<String> | 命名空间(类型 `List`)。The namespace. |
key | String | 键(类型 `String`)。The key. |
io/agentscope/extensions/redis/store/RedisStore.java:188–194