api-base/pkg/redis/redis.go

40 lines
590 B
Go
Raw Normal View History

2020-08-15 17:21:27 +00:00
package redis
import (
"fmt"
"github.com/go-redis/redis"
)
const (
KeyToken = "91-token:%s"
)
type Redis struct {
*redis.Client
prefix string
}
var r *Redis
// Init redis client
func Init(port int, host, prefix string) error {
r = &Redis{prefix: prefix}
r.Client = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", host, port),
})
if _, err := r.Ping().Result(); err != nil {
return err
}
return nil
}
// Key combine prefix to key
func (x *Redis) Key(key string) string {
if len(x.prefix) > 0 {
return fmt.Sprintf("%s:%s", x.prefix, key)
}
return key
}