引用包:“github.com/go-redis/redis/v8”
type Options struct {
Network string
Addr string
Dialer func(ctx context.Context, network string, addr string) (net.Conn, error)
OnConnect func(ctx context.Context, cn *Conn) error
Username string
Password string
DB int
MaxRetries int
MinRetryBackoff time.Duration
MaxRetryBackoff time.Duration
DialTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
PoolFIFO bool
PoolSize int
MinIdleConns int
MaxConnAge time.Duration
PoolTimeout time.Duration
IdleTimeout time.Duration
IdleCheckFrequency time.Duration
readOnly bool
TLSConfig *tls.Config
Limiter Limiter
}
func NewClient(opt *Options) *Client
rdb := redis.NewClient(&redis.Options{
Addr: "10.10.239.136:6379",
Password: "",
DB: 1,
})
package main
import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
)
var ctx = context.Background()
func main() {
rdb := RedisConnect()
fmt.Printf("rdb : %+v", rdb)
}
func RedisConnect() (rdb *redis.Client) {
var myRedis redis.Options
myRedis.Addr = "10.10.239.136:6379"
myRedis.Password = ""
myRedis.DB = 1
rdb = redis.NewClient(&myRedis)
return rdb
}
API server listening at: 127.0.0.1:62059
rdb : Redis<10.10.239.136:6379 db:1>
Debugger finished with the exit code 0
func (c cmdable) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *StatusCmd
err := rdb.Set(ctx, "project:users:{user_name}:string", "GuanYu", time.Minute*2).Err()
expiration 为 0 表示没有过期限制
package main
import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
"time"
)
var ctx = context.Background()
func main() {
rdb := RedisConnect()
fmt.Printf("rdb : %+v", rdb)
err := rdb.Set(ctx, "project:users:{user_name}:string", "GuanYu", time.Minute*2).Err()
if err != nil {
fmt.Println("err: ", err)
} else {
fmt.Println("保存成功")
}
}
func RedisConnect() (rdb *redis.Client) {
//以下这些值在实际应用中,可以从环境变量或配置文件中读取
var myRedis redis.Options
myRedis.Addr = "10.10.239.136:6371"
myRedis.Password = ""
myRedis.DB = 1
rdb = redis.NewClient(&myRedis)
return rdb
}
func (c cmdable) Get(ctx context.Context, key string) *StringCmd
esult := rdb.Get(ctx, "project:users:{user_name}:string")
package main
import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
"time"
)
var ctx = context.Background()
func main() {
rdb := RedisConnect()
fmt.Printf("rdb : %+v", rdb)
//写入
err := rdb.Set(ctx, "project:users:{user_name}:string", "GuanYu", time.Minute*2).Err()
if err != nil {
fmt.Println("err: ", err)
} else {
fmt.Println("保存成功")
}
//查询
result := rdb.Get(ctx, "project:users:{user_name}:string")
err = result.Err()
if err != nil {
fmt.Println("err :", err)
return
}
fmt.Printf("以string完整输出:%s\n", result.String())
fmt.Printf("仅输出值:%s\n", result.Val())
}
func RedisConnect() (rdb *redis.Client) {
var myRedis redis.Options
myRedis.Addr = "10.10.239.136:6379"
myRedis.Password = ""
myRedis.DB = 1
rdb = redis.NewClient(&myRedis)
return rdb
}
以string完整输出:get project:users:{user_name}:string: GuanYu
仅输出值:GuanYu
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/go-redis/redis/v8"
"time"
)
var ctx = context.Background()
type UserName struct {
UserId int64 `json:"user_id"`
UserName string `json:"user_name"`
Age int64 `json:"age"`
}
func main() {
rdb := RedisConnect()
data := &UserName{
UserId: 1,
UserName: "LiuBei",
Age: 28,
}
//结构体转为json字串的[]byte
b, err := json.Marshal(data)
if err != nil {
fmt.Println(err)
}
//写入
err = rdb.Set(ctx, "project:users:{user_name}:struct", string(b), time.Minute*2).Err()
if err != nil {
fmt.Println("err: ", err)
}
//查找
result := rdb.Get(ctx, "project:users:{user_name}:struct")
fmt.Println(result.Val())
}
func RedisConnect() (rdb *redis.Client) {
var myRedis redis.Options
myRedis.Addr = "10.10.239.136:6379"
myRedis.Password = ""
myRedis.DB = 1
rdb = redis.NewClient(&myRedis)
return rdb
}
{"user_id":1,"user_name":"LiuBei","age":28}
MarshalBinary()
go-redis
已经提供了一个解析方法供我们使用。MarshalBinary()
方法UnmarshalBinary()
go-redis
已经提供了一个方法让我们的自定义结构体接收它。MarshalBinary()
方法完整代码
import (
"context"
"encoding/json"
"fmt"
"github.com/go-redis/redis/v8"
)
type myStruct struct {
UserId string `json:"user_id"`
UserName string `json:"user_name"`
Age int64 `json:"age"`
}
//var _ encoding.BinaryMarshaler = new(myStruct)
//var _ encoding.BinaryUnmarshaler = new(myStruct)
func (m *myStruct) MarshalBinary() (data []byte, err error) {
return json.Marshal(m) //解析
}
func (m *myStruct) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, m) //反解析
}
func main() {
var ctx = context.Background()
rdb := RedisConnect()
data := &myStruct{
UserId: "123",
UserName: "GuanYu",
Age: 25,
}
//写入
err := rdb.Set(ctx, "project:users:{user_name}:struct", data, 0).Err()
if err != nil {
fmt.Println(err)
}
//读出
result := &myStruct{}
err = rdb.Get(ctx, "project:users:{user_name}:struct").Scan(result)
if err != nil {
fmt.Println(err)
}
//fmt.Printf("get success: %+v\n", result)
fmt.Println(data)
}
func RedisConnect() (rdb *redis.Client) {
var myRedis redis.Options
myRedis.Addr = "10.10.239.136:6379"
myRedis.Password = ""
myRedis.DB = 1
rdb = redis.NewClient(&myRedis)
return rdb
}
&{UserId:123 UserName:GuanYu Age:25}