Skip to content

Commit 6ec807f

Browse files
committed
db.redis: fix bug in RESP2, add RESP3, and more tests
1 parent 92f9ead commit 6ec807f

File tree

3 files changed

+1204
-111
lines changed

3 files changed

+1204
-111
lines changed

vlib/db/redis/README.md

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Redis Client for V
22

33
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.
4+
Redis Serialization Protocol (RESP) versions 2 (RESP2) and 3 (RESP3) with
5+
a type-safe interface for common Redis commands.
56

67
## Features
78

@@ -12,7 +13,6 @@ Redis Serialization Protocol (RESP) with a type-safe interface for common Redis
1213
- **Memory Efficient**: Pre-allocated buffers for minimal allocations
1314

1415
## Quick Start
15-
1616
```v
1717
module main
1818
@@ -22,10 +22,14 @@ fn main() {
2222
// Connect to Redis
2323
// Uncomment passwod line if authentication is needed
2424
mut db := redis.connect(redis.Config{
25-
host: 'localhost'
26-
// password: 'your_password'
27-
port: 6379
25+
// Available Config options (none of which need to be specified if you want the defauilts):
26+
// host: 'localhost' - default
27+
// password: 'your_password' - no default, you need to supply password if your redis server
28+
// is set up to need one
29+
// port: 6379 - default
30+
// tls: false - default, set to true for ssl connection
2831
})!
32+
println('Server supports RESP${db.version} protocol')
2933
3034
// Set and get values
3135
db.set('name', 'Alice')!
@@ -46,7 +50,6 @@ fn main() {
4650
## Supported Commands
4751

4852
### Key Operations
49-
5053
```v ignore
5154
// Set value
5255
db.set('key', 'value')!
@@ -66,12 +69,11 @@ db.expire('key', 60)! // 60 seconds
6669
```
6770

6871
### Hash Operations
69-
7072
```v ignore
7173
// Set hash fields
7274
db.hset('user:1', {
7375
'name': 'Bob',
74-
'age': 30,
76+
'age': '30',
7577
})!
7678
7779
// Get single field
@@ -83,15 +85,14 @@ println(user_data) // Output: {'name': 'Bob', 'age': '30'}
8385
```
8486

8587
### Pipeline Operations
86-
8788
```v ignore
8889
// Start pipeline
8990
db.pipeline_start()
9091
9192
// Queue commands
92-
db.incr('counter')
93-
db.set('name', 'Charlie')
94-
db.get[string]('name')
93+
db.incr('counter')!
94+
db.set('name', 'Charlie')!
95+
db.get[string]('name')!
9596
9697
// Execute and get responses
9798
responses := db.pipeline_execute()!
@@ -101,7 +102,6 @@ for resp in responses {
101102
```
102103

103104
### Custom Commands
104-
105105
```v ignore
106106
// Run raw commands
107107
resp := db.cmd('SET', 'custom', 'value')!
@@ -129,12 +129,10 @@ result := db.get[string]('nonexistent') or {
129129
```
130130

131131
## Connection Management
132-
133132
```v ignore
134133
config := redis.Config{
135134
host: 'redis.server'
136135
port: 6379
137-
version: 2 // RESP2 protocol
138136
}
139137
140138
mut db := redis.connect(config)!
@@ -148,4 +146,4 @@ defer {
148146
1. **Reuse Connections**: Maintain connections instead of creating new ones
149147
2. **Use Pipelines**: Batch commands for high-throughput operations
150148
3. **Prefer Integers**: Use numeric types for counters and metrics
151-
4. **Specify Types**: Always specify return types for get operations
149+
4. **Specify Types**: Always specify return types for get operations

0 commit comments

Comments
 (0)