Skip to content

Commit ef279f6

Browse files
authored
db: add redis (#24730)
1 parent 9fb205a commit ef279f6

3 files changed

Lines changed: 824 additions & 0 deletions

File tree

‎vlib/db/redis/README.md‎

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# Redis Client for V
2+
3+
This module provides a Redis client implementation in V that supports the
4+
Redis Serialization Protocol (RESP) with a type-safe interface for common Redis commands.
5+
6+
## Features
7+
8+
- **Type-Safe Commands**: Auto-detects value types at compile time
9+
- **Pipeline Support**: Group commands for batch execution
10+
- **Connection Pooling**: Efficient resource management
11+
- **RESP Protocol Support**: Full Redis Serialization Protocol implementation
12+
- **Memory Efficient**: Pre-allocated buffers for minimal allocations
13+
14+
## Quick Start
15+
16+
```v
17+
module main
18+
19+
import db.redis
20+
21+
fn main() {
22+
// Connect to Redis
23+
mut db := redis.connect(redis.Config{
24+
host: 'localhost'
25+
port: 6379
26+
})!
27+
28+
// Set and get values
29+
db.set('name', 'Alice')!
30+
name := db.get[string]('name')!
31+
println('Name: ${name}') // Output: Name: Alice
32+
33+
// Integer operations
34+
db.set('counter', 42)!
35+
db.incr('counter')!
36+
counter := db.get[int]('counter')!
37+
println('Counter: ${counter}') // Output: Counter: 43
38+
39+
// Clean up
40+
db.close()!
41+
}
42+
```
43+
44+
## Supported Commands
45+
46+
### Key Operations
47+
48+
```v ignore
49+
// Set value
50+
db.set('key', 'value')!
51+
db.set('number', 42)!
52+
db.set('binary', []u8{len: 4, init: 0})!
53+
54+
// Get value
55+
str_value := db.get[string]('key')!
56+
int_value := db.get[int]('number')!
57+
bin_value := db.get[[]u8]('binary')!
58+
59+
// Delete key
60+
db.del('key')!
61+
62+
// Set expiration
63+
db.expire('key', 60)! // 60 seconds
64+
```
65+
66+
### Hash Operations
67+
68+
```v ignore
69+
// Set hash fields
70+
db.hset('user:1', {
71+
'name': 'Bob',
72+
'age': 30,
73+
})!
74+
75+
// Get single field
76+
name := db.hget[string]('user:1', 'name')!
77+
78+
// Get all fields
79+
user_data := db.hgetall[string]('user:1')!
80+
println(user_data) // Output: {'name': 'Bob', 'age': '30'}
81+
```
82+
83+
### Pipeline Operations
84+
85+
```v ignore
86+
// Start pipeline
87+
db.pipeline_start()
88+
89+
// Queue commands
90+
db.incr('counter')
91+
db.set('name', 'Charlie')
92+
db.get[string]('name')
93+
94+
// Execute and get responses
95+
responses := db.pipeline_execute()!
96+
for resp in responses {
97+
println(resp)
98+
}
99+
```
100+
101+
### Custom Commands
102+
103+
```v ignore
104+
// Run raw commands
105+
resp := db.cmd('SET', 'custom', 'value')!
106+
result := db.cmd('GET', 'custom')!
107+
108+
// Complex commands
109+
db.cmd('HSET', 'user:2', 'field1', 'value1', 'field2', '42')!
110+
```
111+
112+
## Error Handling
113+
114+
All functions return `!` types and will return detailed errors for:
115+
116+
- Connection issues
117+
- Protocol violations
118+
- Type mismatches
119+
- Redis error responses
120+
- Timeout conditions
121+
122+
```v ignore
123+
result := db.get[string]('nonexistent') or {
124+
println('Key not found')
125+
return
126+
}
127+
```
128+
129+
## Connection Management
130+
131+
```v ignore
132+
config := redis.Config{
133+
host: 'redis.server'
134+
port: 6379
135+
version: 2 // RESP2 protocol
136+
}
137+
138+
mut db := redis.connect(config)!
139+
defer {
140+
db.close() or { eprintln('Error closing connection: $err') }
141+
}
142+
```
143+
144+
## Performance Tips
145+
146+
1. **Reuse Connections**: Maintain connections instead of creating new ones
147+
2. **Use Pipelines**: Batch commands for high-throughput operations
148+
3. **Prefer Integers**: Use numeric types for counters and metrics
149+
4. **Specify Types**: Always specify return types for get operations

0 commit comments

Comments
 (0)