Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Do方法

我们最常用的是Do方法,执行同步指令,通过向Redis Server发送对应的Redis API命令,来使用Redis Server的服务。Do方法最大的特点是内部自行从连接池中获取连接对象,使用完毕后自动丢回连接池中,开发者无需手动调用Close方法,方便使用。

基本使用

Set/Get操作

Code Block
languagego
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
languagego
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
languagexml
value

SetEx操作

Code Block
languagego
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()
		key  = "user_100"
		data err := g.Map{
			"name":  "gf",
			"sex":   0,
			"score": 100,
		}
	)
	_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)
	}
	fmt.Println(value.IsNil())
	fmt.Println(value.String())

	time.Sleep(time.Second)

	vvalue, err := g.Redis().DoGet(ctx, "HMGET", key, "name")
	if err != nil {
		g.Log().Fatal(ctx, err)
	}
	fmt.Println(value.IsNil())
	fmt.Println(vvalue.SliceVal())

	// May Output:
	// [gf]
}
我们可以通过 struct 参数执行HMSET操作。
}

执行后,终端输出:

Code Block
languagexml
false
value
true
<nil>

HSet/HGetAll操作

Code Block
languagego
package main

import (
	"fmt"

	_ "github.com/gogf/gf/v2contrib/nosql/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)
	}

	// retrieve hash map
	vvalue, err := g.Redis().DoHGetAll(ctx,"HMGET", key, "name")
	if err != nil {
		g.Log().Fatal(ctx, err)
	}
	fmt.Println(vvalue.SliceMap())

	// scan Mayto Output:struct
	// [gf]
}

自动序列化/反序列化

当给定的参数为map, slice, struct时,gredis内部支持自动对其使用json序列化,并且读取数据时可使用gvar.Var的转换功能实现反序列化。

map存取

Code Block
languagego
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存取
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
languagexml
map[id:1 name:john score:100]
{
    Id:    1,
    Name:  "john",
    Score: 100,
}

HMSet/HMGet操作

Code Block
languagego
package main

import (
	"fmt"
	"_ "github.com/gogf/gf/v2contrib/nosql/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":   10000 1,
			"name":  "john",
		}
	)

	_, err = g.Redis().Do(ctx, "SET", key, user"score": 100,
	})
	if err != nil {
		panic(g.Log().Fatal(ctx, err)
	}

	// retrieve hash map
	resultvalues, err := g.Redis().DoHMGet(ctx, key, "GETid", key"name")
	if err != nil {
		panic(g.Log().Fatal(ctx, err)
	}
	g.Dump(values.Strings())

	var user2 *User
	if err = result.Struct(&user2); err != nil {
		panic(err)
	}
	fmt.Println(user2.Id, user2.Name)
}}

执行后,终端输出:

Code Block
languagexml
[
    "1",
    "john",
]




Panel
titleContent Menu

Table of Contents