How Chat2DB's Redis Key Management Service Handles Key Operations
Chat2DB's Redis key management service implements a six-layer controller-converter-service-plugin architecture that processes create, update, delete, query, and scan operations through REST endpoints, domain converters, and Lua script executors.
Chat2DB is an open-source database management tool that provides comprehensive Redis support through its dedicated key management service. This service abstracts complex Redis commands into a clean Java API, enabling developers to perform key operations via HTTP endpoints or direct service calls. The implementation follows a strict separation of concerns, moving from web controllers through domain converters to plugin-based Lua script execution.
Architecture of the Chat2DB Redis Key Management Service
The service follows a controller → converter → service → plugin pattern that isolates web concerns from domain logic and Redis-specific implementation details.
The Six-Layer Processing Pipeline
| Layer | Component | Responsibility |
|---|---|---|
| 1 | DbRedisKeyController |
REST entry point exposing HTTP endpoints at /api/redis/*. Receives request DTOs and delegates to the service layer. |
| 2 | RedisKeyConverter |
Translates between external request/response models (KeyCreateRequest, KeyUpdateRequest) and internal domain objects (RedisKey, KeyEntry). |
| 3 | IDbRedisKeyService |
Interface declaring the service contract: create, update, delete, query, scan, and keyDetail. |
| 4 | DbRedisKeyServiceImpl |
Domain service implementation that builds command objects and forwards them to the plugin layer. |
| 5 | RedisKeyOperations |
Plugin façade that encapsulates concrete Redis actions and calls the low-level executor. |
| 6 | RedisScriptExecutor |
Executes actual Redis commands using Lua scripts via methods like createRedisKey and scanRedisKey. |
Source files for these components are located in chat2db-community-server/chat2db-community-web, chat2db-community-domain, and chat2db-community-plugins modules respectively.
End-to-End Key Operation Flow
When creating a Redis key, the request traverses all six layers in a standardized workflow:
- HTTP Request →
POST /api/redis/createreceives aKeyCreateRequestpayload - Controller →
DbRedisKeyControllercallsredisKeyConverter.request2create(request)to convert to a domainRedisKeyobject - Service →
DbRedisKeyServiceImpl.createbuilds aRedisKeyCreateCommandand invokesredisKeyOperations.create(command) - Plugin →
RedisKeyOperations.createtransforms the command into a Lua script call viaRedisScriptExecutor.getInstance().createRedisKey(redisKey) - Execution →
RedisScriptExecutorruns the Lua script on the connected Redis instance and returns anExecuteResponse - Response → The service wraps the result into a
KeyEntryand the controller returnsDataResult<KeyEntry>to the client
The same pattern applies to update, delete, query, and scan operations—each step swaps the specific command and executor method while maintaining the identical architectural flow.
Core Components and Implementation Details
REST Controller Layer
DbRedisKeyController serves as the entry point in chat2db-community-server/chat2db-community-web/src/main/java/ai/chat2db/community/web/api/controller/DbRedisKeyController.java. It exposes six primary endpoints:
POST /api/redis/create– Create new keysPOST /api/redis/update– Modify existing keysPOST /api/redis/delete– Remove keysPOST /api/redis/query– Search keysPOST /api/redis/keys– Scan keyspacePOST /api/redis/key_detail– Retrieve metadata
Each method receives a request DTO, delegates conversion, and calls the appropriate method on IDbRedisKeyService.
Data Conversion Layer
Located at chat2db-community-server/chat2db-community-web/src/main/java/ai/chat2db/community/web/api/converter/redis/RedisKeyConverter.java, the converter handles translation between web-facing DTOs and internal domain models. This keeps the controller thin and isolates transformation logic for requests like KeyCreateRequest, KeyUpdateRequest, and KeyDeleteRequest.
Service Layer Interface
IDbRedisKeyService in chat2db-community-server/chat2db-community-domain/chat2db-community-domain-api/src/main/java/ai/chat2db/community/domain/api/service/db/IDbRedisKeyService.java declares the contract:
KeyEntry create(RedisKey redisKey);
KeyEntry update(RedisKey oldKey, RedisKey newKey);
void delete(String key);
List<KeyEntry> query(String pattern);
List<KeyEntry> scan(String cursor, int count);
KeyEntry keyDetail(String key);
Service Implementation
DbRedisKeyServiceImpl in chat2db-community-server/chat2db-community-domain/chat2db-community-domain-core/src/main/java/ai/chat2db/community/domain/core/impl/db/DbRedisKeyServiceImpl.java implements the interface. Each method constructs a command object (e.g., RedisKeyCreateCommand) and passes it to RedisKeyOperations.
Plugin Facade and Script Execution
RedisKeyOperations in chat2db-community-server/chat2db-community-plugins/chat2db-community-redis/src/main/java/ai/chat2db/plugin/redis/RedisKeyOperations.java acts as the plugin façade. It delegates to RedisScriptExecutor in the same module, which provides methods like createRedisKey, updateRedisKey, deleteRedisKey, scanRedisKey, and getRedisKeyDetail to execute Lua scripts against the Redis instance.
Practical Usage Examples
REST API Examples (cURL)
Create a string key with TTL:
curl -X POST http://localhost:10825/api/redis/create \
-H "Content-Type: application/json" \
-d '{
"key": "myKey",
"type": "STRING",
"value": "hello",
"expire": 3600
}'
Update an existing key:
curl -X POST http://localhost:10825/api/redis/update \
-H "Content-Type: application/json" \
-d '{
"oldRedisKey": {"key":"myKey"},
"newRedisKey": {"key":"myKey","value":"world"}
}'
Delete a key:
curl -X POST http://localhost:10825/api/redis/delete \
-H "Content-Type: application/json" \
-d '{"key":"myKey"}'
Direct Java Service Integration
Autowire the service interface for programmatic access:
@Autowired
private IDbRedisKeyService redisKeyService;
public KeyEntry createStringKey(String name, String value, long ttlSeconds) {
RedisKey redisKey = RedisKey.builder()
.name(name)
.type("STRING")
.value(value)
.expire(ttlSeconds)
.build();
return redisKeyService.create(redisKey);
}
Rename a key using the converter:
public void renameKey(String oldName, String newName) {
KeyUpdateRequest request = new KeyUpdateRequest();
request.setOldRedisKey(new KeyEntry(oldName));
request.setNewRedisKey(new KeyEntry(newName));
redisKeyService.update(redisKeyConverter.request2update(request));
}
Summary
- Chat2DB's Redis key management service uses a strict six-layer architecture separating web, domain, and plugin concerns
- REST endpoints in
DbRedisKeyControllerhandle HTTP requests at/api/redis/*routes - Domain conversion occurs in
RedisKeyConverter, translating between DTOs andRedisKeydomain objects - Service implementation in
DbRedisKeyServiceImplorchestrates command creation and delegates to the plugin layer - Lua script execution via
RedisScriptExecutorperforms actual Redis operations atomically - Dual access patterns support both REST API consumption and direct Java service injection for backend integration
Frequently Asked Questions
What operations does Chat2DB's Redis key management service support?
The service supports six core operations: create, update, delete, query (pattern matching), scan (cursor-based iteration), and key detail retrieval. Each operation follows the same layered architecture from controller through to Lua script execution.
How does Chat2DB execute Redis commands internally?
According to the Chat2DB source code, commands execute through RedisScriptExecutor, which runs Lua scripts via methods like createRedisKey, updateRedisKey, and deleteRedisKey. This approach ensures atomic operations and consistent error handling across different Redis deployments.
Can I use the Redis key management service programmatically without the REST API?
Yes. You can autowire the IDbRedisKeyService interface in your Spring components and call methods like create(RedisKey) or update(oldKey, newKey) directly. This bypasses the controller layer and is useful for backend integrations or custom automation scripts.
Where is the Redis plugin logic located in the Chat2DB repository?
The Redis-specific implementation resides in the chat2db-community-plugins/chat2db-community-redis module. Key files include RedisKeyOperations.java for the plugin façade and RedisScriptExecutor.java for command execution, both under src/main/java/ai/chat2db/plugin/redis/.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →