Class RedisStore

java.lang.Object
io.agentscope.extensions.redis.store.RedisStore
All Implemented Interfaces:
io.agentscope.harness.agent.filesystem.remote.store.BaseStore

public class RedisStore extends Object implements io.agentscope.harness.agent.filesystem.remote.store.BaseStore
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 fields value (JSON-encoded Map<String,Object>) and version (a stringified long).
  • Namespace index <prefix>idx:<ns> — a sorted set (all scores 0) holding every k written under that exact namespace, enabling lexicographic search(java.util.List<java.lang.String>, int, int) via ZRANGEBYLEX without scanning the keyspace.

<ns> is the namespace components joined with "\0".

Concurrency

put(java.util.List<java.lang.String>, java.lang.String, java.util.Map<java.lang.String, java.lang.Object>) and putIfVersion(java.util.List<java.lang.String>, java.lang.String, java.util.Map<java.lang.String, java.lang.Object>, long) both run as a single Lua EVAL, making the version read + hash write + index update one atomic Redis operation. This makes putIfVersion(java.util.List<java.lang.String>, java.lang.String, java.util.Map<java.lang.String, java.lang.Object>, long) safe to use as a distributed compare-and-swap primitive across multiple processes sharing the same Redis instance.

search(java.util.List<java.lang.String>, int, int) is not in the same transaction as put(java.util.List<java.lang.String>, java.lang.String, java.util.Map<java.lang.String, java.lang.Object>): 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 Details

    • DEFAULT_KEY_PREFIX

      public static final String DEFAULT_KEY_PREFIX
      Default Redis key prefix; pick something distinctive enough not to collide with other apps.
      See Also:
  • Constructor Details

    • RedisStore

      public RedisStore(redis.clients.jedis.UnifiedJedis jedis)
      Creates a Redis-backed store with the default key prefix.
      Parameters:
      jedis - initialized Jedis client; must not be null
    • RedisStore

      public RedisStore(redis.clients.jedis.UnifiedJedis jedis, String keyPrefix)
      Creates a Redis-backed store.
      Parameters:
      jedis - initialized Jedis client; must not be null
      keyPrefix - Redis key prefix; trailing ":" is added if absent. If blank, the default is used.
    • RedisStore

      public RedisStore(redis.clients.jedis.UnifiedJedis jedis, String keyPrefix, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
      Creates a Redis-backed store with a custom JSON mapper.
      Parameters:
      jedis - initialized Jedis client; must not be null
      keyPrefix - Redis key prefix; trailing ":" is added if absent
      objectMapper - Jackson mapper used to serialize / deserialize item values
  • Method Details

    • get

      public io.agentscope.harness.agent.filesystem.remote.store.StoreItem get(List<String> namespace, String key)
      Specified by:
      get in interface io.agentscope.harness.agent.filesystem.remote.store.BaseStore
    • put

      public void put(List<String> namespace, String key, Map<String,Object> value)
      Specified by:
      put in interface io.agentscope.harness.agent.filesystem.remote.store.BaseStore
    • putIfVersion

      public boolean putIfVersion(List<String> namespace, String key, Map<String,Object> value, long expectedVersion)
      Specified by:
      putIfVersion in interface io.agentscope.harness.agent.filesystem.remote.store.BaseStore
    • search

      public List<io.agentscope.harness.agent.filesystem.remote.store.StoreItem> search(List<String> namespace, int limit, int offset)
      Specified by:
      search in interface io.agentscope.harness.agent.filesystem.remote.store.BaseStore
    • delete

      public void delete(List<String> namespace, String key)
      Specified by:
      delete in interface io.agentscope.harness.agent.filesystem.remote.store.BaseStore