-
thijsheijden authoredthijsheijden authored
keyvaluedriver.go 1.09 KiB
package keyvaluedriver
import (
"context"
"fmt"
"os"
"query-service/pkg/logger"
"github.com/go-redis/redis/v8"
)
// KeyValueStore models the redis driver
type KeyValueStore struct {
client *redis.Client
}
// NewRedisDriver creates and returns a redis driver
func NewRedisDriver() *KeyValueStore {
return &KeyValueStore{}
}
// Start starts the redis driver
func (d *KeyValueStore) Start() {
// Grab the redis host and port from environment vars
redisAddress := os.Getenv("REDIS_ADDRESS")
// redisPassword := os.Getenv("REDIS_PASSWORD")
// Create redis client
d.client = redis.NewClient(&redis.Options{
Addr: redisAddress,
})
pong := d.client.Ping(context.Background())
logger.Log(fmt.Sprintf("%v", pong))
}
// Get retrieves the value from the redis store that belongs to the given key
func (d *KeyValueStore) Get(key *string) string {
return d.client.Get(context.Background(), *key).Val()
}
// Set sets the key value pair in the redis store
func (d *KeyValueStore) Set(key *string, value *string) error {
status := d.client.Set(context.Background(), *key, *value, 0)
return status.Err()
}