Versions Compared
compared with
Key
- This line was added.
- This line was removed.
- Formatting was changed.
Set/Get
操作
Do
方法
我们最常用的是Do
方法,执行同步指令,通过向Redis Server
发送对应的Redis API
命令,来使用Redis Server
的服务。Do
方法最大的特点是内部自行从连接池中获取连接对象,使用完毕后自动丢回连接池中,开发者无需手动调用Close
方法,方便使用。
基本使用
Code Block | ||
---|---|---|
| ||
package main import ( "fmt" _ "github.com/gogf/gf/v2contrib/frame/g" "github.com/gogf/gf/v2/os/gctx" ) func main() { var ( ctx = gctx.New() ) v, _ := g.Redis().Do(ctx, "SET", "k", "v") fmt.Println(v.String()) } |
HSET/HGETALL
操作
Code Block | ||
---|---|---|
| ||
package main import ( "fmt" "github.com/gogf/gf/v2/container/gvar"nosql/redis/v2" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gctx" ) func main() { var ( err error result *gvar.Var key = "user" ctx = gctx.New() ) _, err := g.Redis().DoSet(ctx, "HSETkey", key, "idvalue", 10000) if err != nil { panic(g.Log().Fatal(ctx, err) } _value, err := g.Redis().DoGet(ctx,"HSET ", key, "name", "john") if err != nil { panic(err) } result, err = g.RedisLog().DoFatal(ctx,"HGETALL", key) if err != nil { panic(err) } fmt.Println(resultvalue.MapString()) // May Output: // map[id:10000 name:john] } |
HMSET/HMGET
操作
我们可以通过map
参数执行HMSET
操作。
执行后,终端输出:
Code Block | ||
---|---|---|
| ||
value |
SetEx
操作
Code Block | ||
---|---|---|
| ||
package main import ( "fmt" "time" _ "github.com/gogf/gf/contrib/v2nosql/frameredis/gv2" "github.com/gogf/gf/v2/osframe/gctxg" "github.com/gogf/gf/v2/utilos/gutilgctx" ) func main() { var ( ctx = gctx.New() keyerr := "user_100" data = g.Map{ "name": "gf", "sex": 0, "score": 100, } ) _g.Redis().SetEX(ctx, "key", "value", 1) if err != nil { g.Log().Fatal(ctx, err) } value, err := g.Redis().DoGet(ctx, "HMSET", append(g.Slice{key}, gutil.MapToSlice(data)...)...)key") if err != nil { g.Log().Fatal(ctx, err) } v fmt.Println(value.IsNil()) fmt.Println(value.String()) time.Sleep(time.Second) value, err := g.Redis().DoGet(ctx, "HMGET", key, "name") if err != nil { g.Log().Fatal(ctx, err) } fmt.Println(v.Slicevalue.IsNil()) fmt.Println(value.Val()) // May Output: // [gf] } |
struct
参数执行HMSET
操作。} |
执行后,终端输出:
Code Block | ||
---|---|---|
| ||
false
value
true
<nil> |
HSet/HGetAll
操作
Code Block | ||
---|---|---|
| ||
package main import ( "fmt" _ "github.com/gogf/gf/contrib/v2nosql/frameredis/gv2" "github.com/gogf/gf/v2/osframe/gctxg" "github.com/gogf/gf/v2/utilos/gutilgctx" ) func main() { type User struct { Name string `json:"name"` Sex int `json:"sex"` Score int `json:"score"` } var ( ctx = gctx.New() key = "user_100key" data = &User) _, err := g.Redis().HSet(ctx, key, g.Map{ Name"id": "gf" 1, Sex"name": 0"john", Score"score": 100, } ) _, err := g.Redis().Do(ctx,"HMSET", append(g.Slice{key}, gutil.StructToSlice(data)...)...) if err != nil { g.Log().Fatal(ctx, err) } v // retrieve hash map value, err := g.Redis().DoHGetAll(ctx,"HMGET", key, "name") if err != nil { g.Log().Fatal(ctx, err) } fmt.Println(vvalue.SliceMap()) // May Output: // [gf] } |
自动序列化/反序列化
当给定的参数为map
, slice
, struct
时,gredis
内部支持自动对其使用json
序列化,并且读取数据时可使用gvar.Var
的转换功能实现反序列化。
map
存取
Code Block | ||
---|---|---|
| ||
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
)
func main() {
var (
ctx = gctx.New()
err error
result *gvar.Var
key = "user"
data = g.Map{
"id": 10000,
"name": "john",
}
)
_, err = g.Redis().Do(ctx, "SET", key, data)
if err != nil {
panic(err)
}
result, err = g.Redis().Do(ctx,"GET", key)
if err != nil {
panic(err)
}
fmt.Println(result.Map())
} |
struct
存取scan to struct
type User struct {
Id uint64
Name string
Score float64
}
var user *User
if err = value.Scan(&user); err != nil {
g.Log().Fatal(ctx, err)
}
g.Dump(user)
} |
执行后,终端输出:
Code Block | ||
---|---|---|
| ||
map[id:1 name:john score:100]
{
Id: 1,
Name: "john",
Score: 100,
} |
HMSet/HMGet
操作
Code Block | ||
---|---|---|
| ||
package main import ( "fmt" _ "github.com/gogf/gf/contrib/v2nosql/containerredis/gvarv2" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gctx" ) func main() { type User struct { Id int Name string } var ( ctx = gctx.New() err error result *gvar.Var key = "userkey" ) usererr := =g.Redis().HMSet(ctx, key, g.Map{ "id": 100001, "name": "john", } ) _, err = g.Redis().Do(ctx, "SET", key, user"score": 100, }) if err != nil { panic(g.Log().Fatal(ctx, err) } result// retrieve hash map values, err := g.Redis().DoHMGet(ctx, key, "GETid", key"name") if err != nil { panic(g.Log().Fatal(ctx, err) } var user2 *User if err = result.Struct(&user2); err != nil { panic(err) } fmt.Println(user2.Id, user2.Name) } g.Dump(values.Strings()) } |
执行后,终端输出:
Code Block | ||
---|---|---|
| ||
[
"1",
"john",
] |
As per Redis 4.0.0, HMSET is considered deprecated. Please use HSET in new code.
根据Redis 4.0.0
,HMSET
被视为已弃用。请在新代码中使用HSET
。
Panel | ||
---|---|---|
| ||
|