A Redis adapter for the Go Persistence API (GPA), providing type-safe Redis operations with compile-time type safety using Go generics.
- Type-safe operations: All key-value operations are type-safe with compile-time checking
- GPA compliance: Implements the Go Persistence API interfaces
- Redis-specific features: TTL support, atomic operations, pattern matching
- Batch operations: Support for multi-get, multi-set, and multi-delete operations
- Flexible configuration: Support for connection URLs and individual parameters
- Connection management: Built-in connection pooling and health checks
go get github.com/lemmego/gparedis
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/lemmego/gpa"
"github.com/lemmego/gparedis"
)
type User struct {
ID string `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
}
func main() {
// Create Redis provider
config := gpa.Config{
Host: "localhost",
Port: 6379,
Database: "0",
}
provider, err := gparedis.NewProvider(config)
if err != nil {
log.Fatal(err)
}
defer provider.Close()
// Get type-safe repository
userRepo := gparedis.GetRepository[User](provider)
// Store a user
user := &User{ID: "123", Name: "John Doe", Age: 30}
if err := userRepo.Set(context.Background(), "user:123", user); err != nil {
log.Fatal(err)
}
// Retrieve a user
retrievedUser, err := userRepo.Get(context.Background(), "user:123")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Retrieved user: %+v\n", retrievedUser)
}
config := gpa.Config{
ConnectionURL: "redis://username:password@localhost:6379/0",
}
config := gpa.Config{
Host: "localhost",
Port: 6379,
Username: "username",
Password: "password",
Database: "0",
}
config := gpa.Config{
Host: "localhost",
Port: 6379,
Options: map[string]interface{}{
"redis": map[string]interface{}{
"max_retries": 3,
"pool_size": 10,
"min_idle_conns": 5,
"dial_timeout": "5s",
"read_timeout": "3s",
"write_timeout": "3s",
"pool_timeout": "4s",
},
},
}
Get(ctx, key)
- Retrieve a value by keySet(ctx, key, value)
- Store a valueDeleteKey(ctx, key)
- Delete a keyKeyExists(ctx, key)
- Check if key exists
MGet(ctx, keys)
- Get multiple valuesMSet(ctx, pairs)
- Set multiple key-value pairsMDelete(ctx, keys)
- Delete multiple keys
SetWithTTL(ctx, key, value, ttl)
- Store with expirationSetTTL(ctx, key, ttl)
- Set TTL for existing keyGetTTL(ctx, key)
- Get remaining TTLRemoveTTL(ctx, key)
- Remove TTL (make persistent)
Increment(ctx, key, delta)
- Atomic incrementDecrement(ctx, key, delta)
- Atomic decrement
Keys(ctx, pattern)
- Get keys matching patternScan(ctx, cursor, pattern, count)
- Scan keys with cursor
- TTL: Time-to-live support for keys
- Atomic Operations: Atomic increment/decrement operations
- Pub/Sub: Redis pub/sub capabilities
- Streaming: Redis streams support
- Transactions: Redis transaction support
The package converts Redis errors to GPA errors with proper error types:
ErrorTypeNotFound
- Key not foundErrorTypeSerialization
- JSON serialization/deserialization errorsErrorTypeDatabase
- General Redis errors
MIT License - see LICENSE.md for details.