ose-redis
is a lightweight Go package for interacting with Redis, built in the ose-micro style.
It provides basic key-value operations with structured logging and OpenTelemetry tracing, making it easy to integrate into microservices.
- Simple key-value operations:
Set
,Get
,Del
,Exists
- TTL support for keys
- OpenTelemetry tracing for all operations
- Structured logging for errors
- Easy to extend for Streams, caching, and locking
go get github.com/ose-micro/ose-redis
package main
import (
"context"
"fmt"
"time"
"github.com/ose-micro/ose-logger"
"github.com/ose-micro/ose-tracer"
"github.com/ose-micro/ose-redis"
)
func main() {
// Setup logger and tracer
log := logger.New()
tr := tracer.New("app")
// Initialize Redis client
client := redis.New(redis.Config{
Addr: "localhost:6379",
DB: 0,
}, log, tr)
defer client.Close()
ctx := context.Background()
// Set a key with TTL
err := client.Set(ctx, "foo", "bar", 10*time.Second)
if err != nil {
log.Error("failed to set key", "error", err)
}
// Get a key
val, err := client.Get(ctx, "foo")
if err != nil {
log.Error("failed to get key", "error", err)
} else {
fmt.Println("foo:", val)
}
// Check if key exists
exists, _ := client.Exists(ctx, "foo")
fmt.Println("exists?", exists)
// Delete a key
client.Del(ctx, "foo")
}
Method | Description | Parameters | Returns |
---|---|---|---|
Set | Set a key with TTL | ctx, key, value, ttl | error |
Get | Get the value of a key | ctx, key | string, error |
Del | Delete a key | ctx, key | error |
Exists | Check if a key exists | ctx, key | bool, error |
Close | Close Redis connection | - | error |
- All operations are traced using OpenTelemetry spans
- Errors are logged with structured logger (ose-core)
Testing
The package includes unit tests using redismock to mock Redis interactions. Run tests with:
go test ./...
MIT License
If you want, I can also create a short version of the README specifically for GitHub packages + go.dev, highlighting just install, usage, and examples — perfect for onboarding other devs quickly.
Do you want me to do that?