mirror of
https://github.com/tiennm99/keepalive.git
synced 2026-10-11 03:13:31 +00:00
Consolidates the *-keepalive family (redis, valkey, postgresql, mysql, mongodb, couchbase) into a single binary with a runtime DB_TYPE dispatch and an Adapter interface. Original repos archived with a pointer here.
39 lines
739 B
Go
39 lines
739 B
Go
package adapter
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
func init() {
|
|
Registry["redis"] = func() (Adapter, error) { return &redisAdapter{}, nil }
|
|
}
|
|
|
|
type redisAdapter struct {
|
|
client *redis.Client
|
|
key string
|
|
}
|
|
|
|
func (a *redisAdapter) Connect(ctx context.Context) error {
|
|
url, err := envOrFail("REDIS_URL")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
opt, err := redis.ParseURL(url)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
a.client = redis.NewClient(opt)
|
|
a.key = envOr("COUNTER_KEY", "counter")
|
|
return a.client.Ping(ctx).Err()
|
|
}
|
|
|
|
func (a *redisAdapter) Increment(ctx context.Context) (int64, error) {
|
|
return a.client.Incr(ctx, a.key).Result()
|
|
}
|
|
|
|
func (a *redisAdapter) Close(_ context.Context) error {
|
|
return a.client.Close()
|
|
}
|