mirror of
https://github.com/kennethreitz/herald.git
synced 2026-06-16 21:01:01 +00:00
45 lines
679 B
Go
45 lines
679 B
Go
package herald
|
|
|
|
import "log"
|
|
import "os"
|
|
import "github.com/garyburd/redigo/redis"
|
|
|
|
// Redis Stuff.
|
|
var REDIS_URL = os.Getenv("REDIS_URL")
|
|
|
|
type Redis struct {
|
|
URL string
|
|
Connection redis.Conn
|
|
}
|
|
|
|
func NewRedis(url string) Redis {
|
|
// Support default $REDIS_URL, if none was provided.
|
|
if url == "" {
|
|
url = REDIS_URL
|
|
}
|
|
|
|
r := Redis{
|
|
URL: url,
|
|
}
|
|
r.Connect()
|
|
return r
|
|
}
|
|
|
|
// Opens (and returns) the Redis connection.
|
|
func (r *Redis) Connect() redis.Conn {
|
|
c, err := redis.DialURL(r.URL)
|
|
r.Connection = c
|
|
|
|
if err != nil {
|
|
// Fail epically.
|
|
log.Fatal(err)
|
|
}
|
|
|
|
return c
|
|
}
|
|
|
|
// Closes the Redis connection.
|
|
func (r *Redis) Close() {
|
|
defer r.Connection.Close()
|
|
}
|