Package com.tnsai.tools.database
Class RedisTools
java.lang.Object
com.tnsai.tools.database.RedisTools
Function-shape Redis tools — the LangChain-style replacement for the legacy
RedisTool that crammed five commands into a single
COMMAND key [value] text protocol (#188 — RFC).
Five typed @Tool methods, each one a separate logical tool with the
correct idempotent flag for the framework's retry / fallback layer:
| Method | Effect | Idempotent |
|---|---|---|
redisGet(java.lang.String) | read value | ✓ |
redisKeys(java.lang.String, int) | list matching keys | ✓ |
redisTtl(java.lang.String) | read remaining TTL | ✓ |
redisSet(java.lang.String, java.lang.String, int) | write value (optional TTL) | ✓ (overwrites) |
redisDel(java.util.List<java.lang.String>) | delete one or more keys | ✓ (idempotent in Redis semantics) |
Both write methods are intentionally exposed — the legacy tool supported
them and removing the capability would break consumers. They carry
"destructive" wording in their @Tool.description so
the LLM treats them with the same caution as a typical "write" tool.
Connection lifecycle
Each call opens its own Jedis client and closes via
try-with-resources. For high-throughput agents use a pooled RedisClient at the app
layer; this tool intentionally stays small and dependency-free.
- Since:
- 0.5.7 (Phase 2 batch 5c — built-in tool migration to function shape, see #188)
-
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor — picksREDIS_URLfrom env.RedisTools(String redisUrl) Explicit URL constructor.RedisTools(String redisUrl, int defaultKeysLimit) Full constructor — URL + per-instance default cap on KEYS results. -
Method Summary
-
Constructor Details
-
RedisTools
public RedisTools()Default constructor — picksREDIS_URLfrom env. -
RedisTools
Explicit URL constructor. -
RedisTools
Full constructor — URL + per-instance default cap on KEYS results.
-
-
Method Details
-
redisGet
@Tool(name="redis_get", description="Get the string value stored at the given Redis key. Returns the value, or the literal string \"(nil)\" when the key does not exist (matches Redis CLI convention). Read-only.", keywords={"redis","cache","get","read"}, idempotent=true) public String redisGet(@ToolParam(description="Redis key to fetch (e.g. \"user:1:name\")") String key) -
redisKeys
@Tool(name="redis_keys", description="List Redis keys matching a glob-style pattern (\"user:*\" matches user:1:name, user:2:name, \u2026). Capped at {limit} to prevent context-blowing scans. Note: KEYS is O(N) \u2014 for production-scale databases prefer SCAN at the app layer. Read-only.", keywords={"redis","cache","keys","list","scan"}, idempotent=true) public List<String> redisKeys(@ToolParam(description="Glob pattern (e.g. \"user:*\", \"session:*:active\", \"*\" for all)") String pattern, @ToolParam(description="Max keys to return (1..10000); 0 falls back to instance default",required=false,defaultValue="0") int limit) -
redisTtl
@Tool(name="redis_ttl", description="Get the remaining time-to-live (in seconds) for a key. Returns {\"ttlSeconds\": N, \"status\": \"ok\"|\"no-expiry\"|\"missing\"} \u2014 Redis\'s raw TTL conventions are translated into a struct the LLM can branch on without remembering -1 / -2. Read-only.", keywords={"redis","cache","ttl","expiry"}, idempotent=true) public Map<String,Object> redisTtl(@ToolParam(description="Redis key to inspect") String key) -
redisSet
@Tool(name="redis_set", description="DESTRUCTIVE: write a string value to a Redis key, OVERWRITING any existing value. Optional {ttlSeconds} schedules automatic expiry (0 / negative = no expiry). Returns the Redis status reply (\"OK\" on success). Use redis_get first if you need to compare or back up the previous value.", keywords={"redis","cache","set","write","store"}, idempotent=true) public String redisSet(@ToolParam(description="Redis key to write") String key, @ToolParam(description="Value to store (string)") String value, @ToolParam(description="Optional TTL in seconds; 0 or negative leaves the key persistent",required=false,defaultValue="0") int ttlSeconds) -
redisDel
@Tool(name="redis_del", description="DESTRUCTIVE: delete one or more Redis keys. Returns {\"deleted\": N} where N is the count of keys that actually existed and were removed. Idempotent: deleting a missing key is a no-op (counted as 0).", keywords={"redis","cache","del","delete","remove"}, idempotent=true) public Map<String,Object> redisDel(@ToolParam(description="Keys to delete (1+); each must be non-blank") List<String> keys)
-