shad-go/lectures/07-sql/redis/redis.go

32 lines
507 B
Go
Raw Normal View History

2020-04-09 13:00:41 +00:00
package redis
import (
"log"
"time"
2021-04-08 11:32:08 +00:00
"github.com/go-redis/redis/v8"
2020-04-09 13:00:41 +00:00
)
2021-04-08 11:32:08 +00:00
func Example(ctx context.Context) {
2020-04-09 13:00:41 +00:00
rdb := redis.NewUniversalClient(&redis.UniversalOptions{
MasterName: "master",
Addrs: []string{":26379"},
})
defer rdb.Close()
2021-04-08 11:32:08 +00:00
if err := rdb.Ping(ctx); err != nil {
2020-04-09 13:00:41 +00:00
log.Fatal(err)
}
2021-04-08 11:32:08 +00:00
if err := rdb.Set(ctx, "key", "value", time.Hour).Err(); err != nil {
2020-04-09 13:00:41 +00:00
log.Fatal(err)
}
2021-04-08 11:32:08 +00:00
value, err := rdb.Get(ctx, "key").Result()
2020-04-09 13:00:41 +00:00
if err != nil {
log.Fatal(err)
}
log.Println(value)
}