1

I want to create a generic Redis interface for storing and getting values. I am beginner to Golang and redis. If there are any changes to be done to the code I would request you to help me.

package main

import (
    "fmt"

    "github.com/go-redis/redis"
)

func main() {

    student := map[string]string{
        "id":   "st01",
        "name": "namme1",
    }

    set("key1", student, 0)
    get("key1")

}

func set(key string, value map[string]string, ttl int) bool {
    client := redis.NewClient(&redis.Options{
        Addr:     "localhost:6379",
        Password: "",
        DB:       0,
    })

    err := client.Set(key, value, 0).Err()
    if err != nil {
        fmt.Println(err)
        return false
    }
    return true
}

func get(key string) bool {
    client := redis.NewClient(&redis.Options{
        Addr:     "localhost:6379",
        Password: "",
        DB:       0,
    })

    val, err := client.Get(key).Result()
    if err != nil {
        fmt.Println(err)
        return false
    }
    fmt.Println(val)
    return true
}

When i run this code i receive an error of "redis: can't marshal map[string]string (implement encoding.BinaryMarshaler)". I have tried using marshal but there was no use. I would request you to help me with this.

Black Knight
  • 37
  • 1
  • 8
  • 1
    Does this answer your question? [Can't marshal, (implement encoding.BinaryMarshaler). go-redis Sdd with multiple objects](https://stackoverflow.com/questions/60925203/cant-marshal-implement-encoding-binarymarshaler-go-redis-sdd-with-multiple) – kingkupps Nov 16 '21 at 15:27
  • I tried it but i received the same error after adding and changing the code. – Black Knight Nov 16 '21 at 15:34
  • 1
    @BlackKnight show the code with your attempt to implement and use the `encoding.BinaryMarshaler`. – mkopriva Nov 16 '21 at 15:55

1 Answers1

5

The non-scalar type of go cannot be directly converted to the storage structure of redis, so the structure needs to be converted before storage

If you want to implement a general method, then the method should receive a type that can be stored directly, and the caller is responsible for converting the complex structure into a usable type, for example:

// ...

    student := map[string]string{
        "id":   "st01",
        "name": "namme1",
    }

    // Errors should be handled here
    bs, _ := json.Marshal(student)

    set("key1", bs, 0)

// ...

func set(key string, value interface{}, ttl int) bool {
    // ...
}

A specific method can structure a specific structure, but the structure should implement the serializers encoding.MarshalBinary and encoding.UnmarshalBinary, for example:

type Student map[string]string

func (s Student) MarshalBinary() ([]byte, error) {
    return json.Marshal(s)
}

// make sure the Student interface here accepts a pointer
func (s *Student) UnmarshalBinary(data []byte) error {
    return json.Unmarshal(data, s)
}

// ...

    student := Student{
        "id":   "st01",
        "name": "namme1",
    }

    set("key1", student, 0)

// ...

func set(key string, value Student, ttl int) bool {
    // ...
}
Community
  • 1
  • 1
Luna
  • 2,132
  • 2
  • 7
  • 14