mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
4
example/nosql/redis/configuration/basic/config.yaml
Normal file
4
example/nosql/redis/configuration/basic/config.yaml
Normal file
@ -0,0 +1,4 @@
|
||||
redis:
|
||||
default:
|
||||
address: 127.0.0.1:6379
|
||||
db: 1
|
||||
23
example/nosql/redis/configuration/basic/main.go
Normal file
23
example/nosql/redis/configuration/basic/main.go
Normal file
@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
_ "github.com/gogf/gf/contrib/nosql/redis/v2"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var ctx = gctx.New()
|
||||
_, err := g.Redis().Set(ctx, "key", "value")
|
||||
if err != nil {
|
||||
g.Log().Fatal(ctx, err)
|
||||
}
|
||||
value, err := g.Redis().Get(ctx, "key")
|
||||
if err != nil {
|
||||
g.Log().Fatal(ctx, err)
|
||||
}
|
||||
fmt.Println(value.String())
|
||||
}
|
||||
34
example/nosql/redis/configuration/senior/main.go
Normal file
34
example/nosql/redis/configuration/senior/main.go
Normal file
@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
_ "github.com/gogf/gf/contrib/nosql/redis/v2"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gredis"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
)
|
||||
|
||||
var (
|
||||
config = gredis.Config{
|
||||
Address: "127.0.0.1:6379",
|
||||
Db: 1,
|
||||
}
|
||||
group = "cache"
|
||||
ctx = gctx.New()
|
||||
)
|
||||
|
||||
func main() {
|
||||
gredis.SetConfig(&config, group)
|
||||
|
||||
_, err := g.Redis(group).Set(ctx, "key", "value")
|
||||
if err != nil {
|
||||
g.Log().Fatal(ctx, err)
|
||||
}
|
||||
value, err := g.Redis(group).Get(ctx, "key")
|
||||
if err != nil {
|
||||
g.Log().Fatal(ctx, err)
|
||||
}
|
||||
fmt.Println(value.String())
|
||||
}
|
||||
4
example/nosql/redis/expire/config.yaml
Normal file
4
example/nosql/redis/expire/config.yaml
Normal file
@ -0,0 +1,4 @@
|
||||
redis:
|
||||
default:
|
||||
address: 127.0.0.1:6379
|
||||
db: 1
|
||||
34
example/nosql/redis/expire/main.go
Normal file
34
example/nosql/redis/expire/main.go
Normal file
@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
_ "github.com/gogf/gf/contrib/nosql/redis/v2"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var ctx = gctx.New()
|
||||
err := g.Redis().SetEX(ctx, "key", "value", 1)
|
||||
if err != nil {
|
||||
g.Log().Fatal(ctx, err)
|
||||
}
|
||||
value, err := g.Redis().Get(ctx, "key")
|
||||
if err != nil {
|
||||
g.Log().Fatal(ctx, err)
|
||||
}
|
||||
fmt.Println(value.IsNil())
|
||||
fmt.Println(value.String())
|
||||
|
||||
time.Sleep(time.Second)
|
||||
|
||||
value, err = g.Redis().Get(ctx, "key")
|
||||
if err != nil {
|
||||
g.Log().Fatal(ctx, err)
|
||||
}
|
||||
fmt.Println(value.IsNil())
|
||||
fmt.Println(value.Val())
|
||||
}
|
||||
4
example/nosql/redis/hmset/config.yaml
Normal file
4
example/nosql/redis/hmset/config.yaml
Normal file
@ -0,0 +1,4 @@
|
||||
redis:
|
||||
default:
|
||||
address: 127.0.0.1:6379
|
||||
db: 1
|
||||
30
example/nosql/redis/hmset/main.go
Normal file
30
example/nosql/redis/hmset/main.go
Normal file
@ -0,0 +1,30 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
_ "github.com/gogf/gf/contrib/nosql/redis/v2"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var (
|
||||
ctx = gctx.New()
|
||||
channel = "channel"
|
||||
)
|
||||
conn, _ := g.Redis().Conn(ctx)
|
||||
defer conn.Close(ctx)
|
||||
_, err := conn.Subscribe(ctx, channel)
|
||||
if err != nil {
|
||||
g.Log().Fatal(ctx, err)
|
||||
}
|
||||
for {
|
||||
msg, err := conn.ReceiveMessage(ctx)
|
||||
if err != nil {
|
||||
g.Log().Fatal(ctx, err)
|
||||
}
|
||||
fmt.Println(msg.Payload)
|
||||
}
|
||||
}
|
||||
4
example/nosql/redis/hset/config.yaml
Normal file
4
example/nosql/redis/hset/config.yaml
Normal file
@ -0,0 +1,4 @@
|
||||
redis:
|
||||
default:
|
||||
address: 127.0.0.1:6379
|
||||
db: 1
|
||||
44
example/nosql/redis/hset/main.go
Normal file
44
example/nosql/redis/hset/main.go
Normal file
@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
_ "github.com/gogf/gf/contrib/nosql/redis/v2"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var (
|
||||
ctx = gctx.New()
|
||||
key = "key"
|
||||
)
|
||||
_, err := g.Redis().HSet(ctx, key, g.Map{
|
||||
"id": 1,
|
||||
"name": "john",
|
||||
"score": 100,
|
||||
})
|
||||
if err != nil {
|
||||
g.Log().Fatal(ctx, err)
|
||||
}
|
||||
|
||||
// retrieve hash map
|
||||
value, err := g.Redis().HGetAll(ctx, key)
|
||||
if err != nil {
|
||||
g.Log().Fatal(ctx, err)
|
||||
}
|
||||
fmt.Println(value.Map())
|
||||
|
||||
// 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)
|
||||
}
|
||||
4
example/nosql/redis/key-value/config.yaml
Normal file
4
example/nosql/redis/key-value/config.yaml
Normal file
@ -0,0 +1,4 @@
|
||||
redis:
|
||||
default:
|
||||
address: 127.0.0.1:6379
|
||||
db: 1
|
||||
23
example/nosql/redis/key-value/main.go
Normal file
23
example/nosql/redis/key-value/main.go
Normal file
@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
_ "github.com/gogf/gf/contrib/nosql/redis/v2"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var ctx = gctx.New()
|
||||
_, err := g.Redis().Set(ctx, "key", "value")
|
||||
if err != nil {
|
||||
g.Log().Fatal(ctx, err)
|
||||
}
|
||||
value, err := g.Redis().Get(ctx, "key")
|
||||
if err != nil {
|
||||
g.Log().Fatal(ctx, err)
|
||||
}
|
||||
fmt.Println(value.String())
|
||||
}
|
||||
Reference in New Issue
Block a user