set.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "github.com/redis/go-redis/v9"
  7. )
  8. func getClient(host, key string) {
  9. ctx := context.Background()
  10. add := fmt.Sprintf("%s:6379", host)
  11. rdb := redis.NewClient(&redis.Options{
  12. Addr: add,
  13. Password: "", // no password set
  14. DB: 0, // use default DB
  15. })
  16. val, err := rdb.Get(ctx, key).Result()
  17. if err != nil {
  18. panic(err)
  19. }
  20. fmt.Printf("%s\n", val)
  21. }
  22. func setClient(host, key string) {
  23. ctx := context.Background()
  24. add := fmt.Sprintf("%s:6379", host)
  25. rdb := redis.NewClient(&redis.Options{
  26. Addr: add,
  27. Password: "", // no password set
  28. DB: 0, // use default DB
  29. })
  30. err := rdb.Set(ctx, key, "value", 0).Err()
  31. if err != nil {
  32. panic(err)
  33. }
  34. }
  35. func main() {
  36. if len(os.Args) == 4 {
  37. if v := os.Args[1]; v == "set" {
  38. // The 2nd parameter is MQTT broker server address
  39. setClient(os.Args[2], os.Args[3])
  40. }
  41. if v := os.Args[1]; v == "get" {
  42. // The 2nd parameter is MQTT broker server address
  43. getClient(os.Args[2], os.Args[3])
  44. }
  45. }
  46. }