Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Reproduces issue #23 from radix.v2
package main
import (
"github.com/mediocregopher/radix.v2/pool"
"github.com/op/go-logging"
"errors"
)
var logger = logging.MustGetLogger("issue23_ssce")
// stores the secret in Redis
func storeData(redis *pool.Pool, key string, value string) error {
conn, err := redis.Get()
if err != nil {
return errors.New("Redis connection pool exhausted")
}
defer redis.Put(conn)
if _, err := conn.Cmd("SET", key, value, "NX", "EX", 60*60*24).Str(); err != nil {
return errors.New("Could not store value")
}
return nil
}
// retrieves the secret from Redis, deleting it at the same time
func retrieveData(redis *pool.Pool, key string) (string, error) {
conn, err := redis.Get()
if err != nil {
return "", errors.New("Redis connection pool exhausted")
}
defer redis.Put(conn)
logger.Debug("Reading from Redis key", key)
if data, err := conn.Cmd("GET", key).Str(); err != nil {
logger.Error("Could not read key:", key, " - ", err)
return "", errors.New("Unable to read key")
} else {
if _, delErr := conn.Cmd("DEL", key).Str(); delErr != nil {
// https://github.com/mediocregopher/radix.v2/issues/23
logger.Error("Could not delete data: ", delErr)
}
return data, nil
}
}
// application entry point
func main() {
redis, err := pool.New("tcp", "localhost:6379", 10)
if err != nil {
logger.Fatal("Cannot connect to Redis")
}
const key = "foo_key"
const value = "bar_value"
if err := storeData(redis, key, value); err != nil {
logger.Fatal("Could not store data")
}
if data, err := retrieveData(redis, "foo_key"); err != nil {
logger.Fatal("Could not retrieve data")
} else {
logger.Info("Read data:", data)
if data == value {
logger.Info("All is well that ends well")
} else {
logger.Fatal("Oops, now we're in a bit of a pickle")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.