Set/Get Operation

  1. package main
  2. import (
  3. "fmt"
  4. _ "github.com/gogf/gf/contrib/nosql/redis/v2"
  5. "github.com/gogf/gf/v2/frame/g"
  6. "github.com/gogf/gf/v2/os/gctx"
  7. )
  8. func main() {
  9. var ctx = gctx.New()
  10. _, err := g.Redis().Set(ctx, "key", "value")
  11. if err != nil {
  12. g.Log().Fatal(ctx, err)
  13. }
  14. value, err := g.Redis().Get(ctx, "key")
  15. if err != nil {
  16. g.Log().Fatal(ctx, err)
  17. }
  18. fmt.Println(value.String())
  19. }

After execution, terminal output:

SetEx Operation

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. _ "github.com/gogf/gf/contrib/nosql/redis/v2"
  6. "github.com/gogf/gf/v2/frame/g"
  7. "github.com/gogf/gf/v2/os/gctx"
  8. )
  9. func main() {
  10. var ctx = gctx.New()
  11. err := g.Redis().SetEX(ctx, "key", "value", 1)
  12. if err != nil {
  13. g.Log().Fatal(ctx, err)
  14. }
  15. value, err := g.Redis().Get(ctx, "key")
  16. if err != nil {
  17. g.Log().Fatal(ctx, err)
  18. }
  19. fmt.Println(value.IsNil())
  20. fmt.Println(value.String())
  21. time.Sleep(time.Second)
  22. value, err = g.Redis().Get(ctx, "key")
  23. if err != nil {
  24. g.Log().Fatal(ctx, err)
  25. }
  26. fmt.Println(value.IsNil())
  27. fmt.Println(value.Val())
  28. }

After execution, terminal output:

  1. value
  2. true
  3. <nil>

HSet/HGetAll Operation

  1. package main
  2. import (
  3. "fmt"
  4. _ "github.com/gogf/gf/contrib/nosql/redis/v2"
  5. "github.com/gogf/gf/v2/frame/g"
  6. "github.com/gogf/gf/v2/os/gctx"
  7. )
  8. func main() {
  9. var (
  10. ctx = gctx.New()
  11. key = "key"
  12. )
  13. _, err := g.Redis().HSet(ctx, key, g.Map{
  14. "id": 1,
  15. "name": "john",
  16. "score": 100,
  17. })
  18. if err != nil {
  19. g.Log().Fatal(ctx, err)
  20. }
  21. // retrieve hash map
  22. value, err := g.Redis().HGetAll(ctx, key)
  23. if err != nil {
  24. g.Log().Fatal(ctx, err)
  25. }
  26. fmt.Println(value.Map())
  27. // scan to struct
  28. type User struct {
  29. Id uint64
  30. Name string
  31. Score float64
  32. }
  33. var user *User
  34. if err = value.Scan(&user); err != nil {
  35. g.Log().Fatal(ctx, err)
  36. }
  37. g.Dump(user)
  38. }

After execution, terminal output:

  1. map[id:1 name:john score:100]
  2. {
  3. Id: 1,
  4. Name: "john",
  5. Score: 100,
  6. }

HMSet/HMGet Operation

  1. package main
  2. import (
  3. _ "github.com/gogf/gf/contrib/nosql/redis/v2"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/os/gctx"
  6. )
  7. func main() {
  8. var (
  9. ctx = gctx.New()
  10. key = "key"
  11. )
  12. err := g.Redis().HMSet(ctx, key, g.Map{
  13. "id": 1,
  14. "name": "john",
  15. "score": 100,
  16. })
  17. if err != nil {
  18. g.Log().Fatal(ctx, err)
  19. }
  20. // retrieve hash map
  21. values, err := g.Redis().HMGet(ctx, key, "id", "name")
  22. if err != nil {
  23. g.Log().Fatal(ctx, err)
  24. }
  25. g.Dump(values.Strings())
  26. }

After execution, terminal output:

  1. [
  2. "1",
  3. "john",
  4. ]

As per Redis 4.0.0, HMSET is considered deprecated. Please use HSET in new code.