Files
RedisManager/README.md
GuilhermeStrice f37078157d add project
2025-07-09 19:31:34 +01:00

462 lines
12 KiB
Markdown

# RedisManager
A comprehensive Redis management tool with a modern daemon/client architecture. All Redis commands, configuration, and management operations are routed through a persistent background daemon (service), ensuring consistent state and robust multi-instance support.
---
## Daemon/Client Architecture
- **Daemon/Service:** A long-running background process (TCP server) that manages all Redis/Valkey instances, configuration persistence, and command execution.
- **Client CLI:** The command-line interface only sends commands to the daemon; it does not interact with Redis directly.
- **Standalone CLI mode is no longer supported.**
- All commands (except `--help`) require the daemon to be running.
---
## Quick Start
1. Install .NET 9.0 or later and Redis server(s) to connect to.
2. Build the project with `dotnet build` in the RedisManager directory.
3. Start the daemon with `dotnet run` in the RedisManager directory.
4. Run commands with `dotnet run -- <command>`.
---
## Features
### Core Functionality
- **Multi-Mode Operation**: Standalone CLI, Service mode, and Client mode
- **Instance Management**: Add, update, delete, and list Redis instances
- **Comprehensive Redis Commands**: Support for all major Redis data types and operations
- **Formatted Output**: Table formatting, JSON pretty-printing, and colored output
- **Persistent Redis Configuration**: Set, list, and persist custom Redis config parameters per instance; parameters are automatically applied on instance startup
- **Configuration Management**: JSON-based configuration with automatic defaults
### Supported Redis Operations
#### String Operations
- `get` - Retrieve key values with JSON formatting
- `set` - Set key values with optional TTL
- `del` - Delete keys with confirmation prompts
- Advanced operations: `append`, `incr`, `decr`, `getrange`, `setrange`, `strlen`, `mget`, `mset`
#### Hash Operations
- `hget`, `hset`, `hdel`, `hgetall`, `hkeys`, `hvals`, `hlen`, `hexists`
- `hincrby`, `hincrbyfloat`, `hmset`, `hmget`, `hsetnx`, `hstrlen`, `hscan`
#### List Operations
- `lpush`, `rpush`, `lpop`, `rpop`, `lrange`, `llen`, `lindex`, `lset`
- `linsert`, `lrem`, `ltrim`, `blpop`, `brpop`, `rpoplpush`
#### Set Operations
- `sadd`, `srem`, `smembers`, `scard`, `sismember`, `srandmember`, `spop`
- `sinter`, `sunion`, `sdiff`, `sinterstore`, `sunionstore`, `sdiffstore`, `sscan`, `smove`
#### Sorted Set Operations
- `zadd`, `zrem`, `zrange`, `zrevrange`, `zrangebyscore`, `zcard`, `zscore`
- `zrank`, `zrevrank`, `zincrby`, `zrevrangebyscore`, `zcount`
- `zunionstore`, `zinterstore`, `zscan`, `zpopmax`, `zpopmin`
- `zremrangebyrank`, `zremrangebyscore`
#### Key Management
- `exists`, `expire`, `ttl`, `persist`, `rename`, `type`, `keys`, `scan`
#### Database Operations
- `select`, `flushdb`, `flushall`, `dbsize`
#### Server Information
- `info`, `config`, `save`, `bgsave`, `lastsave`, `time`, `ping`
#### Persistent Config Management
- `config --set` - Persistently set a Redis config parameter for an instance (e.g., maxmemory, save, etc.)
- `config --list-custom` - List all custom config parameters persisted for an instance
- Custom config parameters are saved in `redismanager.json` and automatically applied to the Redis/Valkey server on instance startup
#### Connection Management
- `auth`, `quit`, `client list`, `client kill`, `client getname`, `client setname`
#### Pub/Sub
- `publish`, `subscribe`, `unsubscribe`
#### Scripting
- `eval`, `evalsha`, `script load`, `script exists`, `script flush`
#### Transactions
- `multi`, `exec`, `discard`, `watch`, `unwatch`
#### Geographic Operations
- `geoadd`, `geodist`, `geohash`, `geopos`, `georadius`
#### Bit Operations
- `bitcount`, `bitfield`, `bitop`, `bitpos`, `getbit`, `setbit`
#### HyperLogLog
- `pfadd`, `pfcount`, `pfmerge`
#### Streams
- `xadd`, `xrange`, `xlen`, `xdel`
#### Module Management
- `module list`, `module load`, `module unload`
## Installation
### Prerequisites
- .NET 9.0 or later
- Redis server(s) to connect to
### Build
```bash
cd RedisManager
dotnet build
```
## Usage
---
## Command Usage Examples
Below are examples for all major command types. All commands must be run with the daemon/service running.
### String Commands
```bash
# Set a value
dotnet run -- set -i default mykey "hello world"
# Get a value
dotnet run -- get -i default mykey
# Delete a key
dotnet run -- del -i default mykey
```
### Hash Commands
```bash
# Set multiple fields
dotnet run -- hset -i default user:1 name "Alice" age "30"
# Get all fields
dotnet run -- hgetall -i default user:1
# Get a field
dotnet run -- hget -i default user:1 name
```
### List Commands
```bash
# Push to a list
dotnet run -- lpush -i default mylist "one" "two"
# Get range
dotnet run -- lrange -i default mylist 0 -1
```
### Set Commands
```bash
# Add members
dotnet run -- sadd -i default myset "a" "b"
# List members
dotnet run -- smembers -i default myset
```
### Sorted Set Commands
```bash
# Add with scores
dotnet run -- zadd -i default myzset 1 "a" 2 "b"
# Range by score
dotnet run -- zrangebyscore -i default myzset 0 10
```
### Key Management
```bash
# List keys
dotnet run -- keys -i default "*"
# Set expiration
dotnet run -- expire -i default mykey 60
```
### Database Commands
```bash
# Select DB
dotnet run -- select -i default 0
# Get DB size
dotnet run -- dbsize -i default
```
### Server Information
```bash
# Get server info
dotnet run -- info -i default
# Ping server
dotnet run -- ping -i default
```
### Persistent Config Management
```bash
# Set and persist a config parameter
dotnet run -- config --instance default --set "maxmemory 128mb"
# List custom config parameters
dotnet run -- config --instance default --list-custom
# Get current value from Redis
dotnet run -- config --instance default --get maxmemory
```
### Pub/Sub
```bash
# Publish a message
dotnet run -- publish -i default mychannel "hello"
```
### Scripting
```bash
# Run a Lua script
dotnet run -- eval -i default "return redis.call('set', KEYS[1], ARGV[1])" 1 mykey myvalue
```
### Transactions
```bash
# Start a transaction
dotnet run -- multi -i default
# Execute transaction
dotnet run -- exec -i default
```
### Geo Commands
```bash
# Add geo data
dotnet run -- geoadd -i default places 13.361389 38.115556 "Palermo"
```
### Bit Operations
```bash
# Set and get bit
dotnet run -- setbit -i default mykey 7 1
dotnet run -- getbit -i default mykey 7
```
### Module Management
```bash
# List modules
dotnet run -- module list -i default
```
### Instance Management
```bash
# List instances
dotnet run -- list-instances
# Add a new instance
dotnet run -- add-instance -n newinst -h localhost -p 6381
# Delete an instance
dotnet run -- delete-instance -n newinst
```
---
### Service (Daemon) Mode
Run as a TCP service that accepts client connections:
```bash
# Start the daemon/service
cd RedisManager
dotnet run
# The service runs on port 6380 by default
```
### Client Mode
All commands must be executed via the daemon:
```bash
# Set a value
cd RedisManager
dotnet run -- set -i default mykey "hello world"
# Get a value
dotnet run -- get -i default mykey
# List all configured instances
dotnet run -- list-instances
```
> **Note:** The daemon/service must be running for any command to work (except --help).
### Service Mode
Run as a TCP service that accepts client connections:
```bash
# Start the service
dotnet run -- --service
# The service runs on port 6379 by default
# Clients can connect and send JSON-formatted requests
```
### Client Mode
Connect to a running service instance:
```bash
# Execute commands through the service
dotnet run -- --client get -i default mykey
dotnet run -- --client set -i default mykey "value"
```
## Configuration
The tool uses a `redismanager.json` configuration file that is automatically created with default settings:
```json
{
"Instances": [
{
"Name": "default",
"Host": "localhost",
"Port": 6379,
"Password": null,
"CustomConfig": {
"maxmemory": "128mb"
}
}
]
}
```
- The `CustomConfig` dictionary stores all persistent Redis config parameters for each instance.
- When the daemon or instance starts, all parameters in `CustomConfig` are automatically applied to the Redis/Valkey server.
- You can edit these parameters using the `config --set` and `config --list-custom` commands.
### Persistent Redis Config Commands
```bash
# Persistently set a Redis config parameter (e.g., maxmemory) for an instance
# This will save the parameter in redismanager.json and apply it on instance startup
dotnet run -- config --instance default --set "maxmemory 128mb"
# List all custom config parameters persisted for an instance
dotnet run -- config --instance default --list-custom
# Get the current value of a config parameter from the running Redis instance
dotnet run -- config --instance default --get maxmemory
```
### Instance Management Commands
```bash
# List all configured instances
dotnet run -- list-instances
# Add a new instance
dotnet run -- add-instance -n production -h redis.prod.com -p 6379 -w "password"
# Update an existing instance
dotnet run -- update-instance -n production -h new-redis.prod.com
# Delete an instance
dotnet run -- delete-instance -n production
```
## Output Formats
### Table Format
Many commands support `--table` output for structured data:
```bash
dotnet run -- get -i default mykey --table
```
### JSON Formatting
String values that appear to be JSON are automatically pretty-printed.
### Colored Output
- Green: Success messages and data
- Red: Error messages
- Yellow: Warnings and confirmations
- Blue: Headers and navigation
## Architecture
### Core Components
#### Program.cs
Main entry point that handles three operational modes:
- **Standalone Mode**: Direct command execution
- **Service Mode**: TCP server for client connections
- **Client Mode**: Client for communicating with service
#### RedisManagerService.cs
TCP server implementation that:
- Accepts client connections
- Processes JSON-formatted requests
- Executes Redis commands
- Returns formatted responses
#### RedisManagerClient.cs
Client implementation that:
- Connects to the service
- Sends command requests
- Displays responses
#### Command Classes
Organized by Redis data type and functionality:
- `StringCommands.cs` - Basic string operations
- `HashCommands.cs` - Hash operations
- `ListCommands.cs` - List operations
- `SetCommands.cs` - Set operations
- `SortedSetCommands.cs` - Sorted set operations
- `KeyCommands.cs` - Key management
- `DatabaseCommands.cs` - Database operations
- `ServerCommands.cs` - Server information
- `ConnectionCommands.cs` - Connection management
- `PubSubCommands.cs` - Pub/Sub operations
- `ScriptingCommands.cs` - Lua scripting
- `TransactionCommands.cs` - Transaction support
- `GeoCommands.cs` - Geographic operations
- `BitCommands.cs` - Bit operations
- `HyperLogLogCommands.cs` - HyperLogLog operations
- `StreamCommands.cs` - Stream operations
- `ModuleCommands.cs` - Module management
- `AdvancedStringCommands.cs` - Advanced string operations
- `InstanceCommands.cs` - Instance management
- `StatusCommand.cs` - Connection status
#### Utility Classes
##### RedisUtils.cs
Provides common Redis functionality:
- Instance configuration retrieval
- Redis connection establishment
- Data type conversions
- Table formatting
##### Output.cs
ANSI color formatting utilities for console output.
##### Config.cs
Configuration management:
- `InstanceConfig` - Individual Redis instance settings
- `Config` - Main configuration container
- `ConfigManager` - Configuration loading/saving utilities
## Error Handling
The tool provides comprehensive error handling:
- Connection failures with helpful messages
- Invalid command syntax with usage hints
- Missing instances with configuration guidance
- Redis operation failures with detailed error messages
## Security Considerations
- Passwords are stored in plain text in the configuration file
- No encryption for client-server communication
- Consider using Redis ACLs for production environments
- Use secure connections (TLS) for sensitive data
## Contributing
1. Fork the repository
2. Create a feature branch
3. Add comprehensive documentation for new commands
4. Ensure all tests pass
5. Submit a pull request
## License
This project is part of a larger trading platform and follows the same licensing terms.