package utils
import (
"log"
"github.com/gomodule/redigo/redis"
)
//
// defer client.Close()
func ConnectRedis() redis.Conn {
client, err := redis.Dial("tcp", "localhost:6379", redis.DialPassword("123456"))
if err != nil {
log.Fatalln(err)
}
return client
}
// 获取字符串
// utils.SetString("hello", "word")
func GetString(key string) string {
client := ConnectRedis()
defer client.Close()
rec1, err := client.Do("Get", key)
if err != nil {
log.Fatal(err)
}
return string(rec1.([]byte))
}
// 设置字符串
// res := utils.GetString("hello")
func SetString(key string, val string) {
client := ConnectRedis()
defer client.Close()
_, err := client.Do("Set", key, val)
if err != nil {
log.Fatal(err)
}
}