Home > Blockchain >  Golang - Ristretto Cache returning base 64
Golang - Ristretto Cache returning base 64

Time:11-14

I am encountering a problem while using ristretto cache. Indeed, i have a little api that should return me a value stored in my ristretto cache as json.

The problem is that when i call my function, the return is the json encoded in base64 and i just can't find the way to decode it.

Here is the code i have:

  • Part 1: the code for initializing my ristretto cache:
func InitCache() {
    var err error
    ristrettoCache, err = ristretto.NewCache(&ristretto.Config{
        NumCounters: 3000,
        MaxCost: 1e6,
        BufferItems: 64,
    })
    if err != nil {
        panic(err)
    }
}

Part 2: Putting my values in cache:

for _, t := range listTokensFromDB {
    b, err := json.Marshal(t)
    if err != nil {
        fmt.Println(err)
    }
    ristrettoCache.Set(t.Symbol, b, 1)
}

Part 3: getting the value from cache

func getTokenInfo(w http.ResponseWriter, r *http.Request){
    vars := mux.Vars(r)
    key := vars["chain"] vars["symbol"]
    value, found := ristrettoCache.Get(key)
    if !found {
        return
    }
    json.NewEncoder(w).Encode(value)
}

The result i have when i make a call to my api is:

"eyJTeW1ib2wiOiJic2NDUllQVE8iLCJBZGRyIjoiMHgyQmNBMUFlM0U1MjQ0NzMyM0IzRWE0NzA4QTNkMTg1ODRDYWY4NWE3IiwiTHBzIjpbeyJTeW1ib2xUb2tlbiI6IkZFRyIsIlRva2VuQWRkciI6IjB4YWNGQzk1NTg1RDgwQWI2MmY2N0ExNEM1NjZDMWI3YTQ5RmU5MTE2NyIsIkxwQWRkciI6IjB4NDU5ZTJlMjQ4NGNlMDU2MWRmNTJiYzFlNjkxMzkyNDA2M2JhZDM5MCJ9LHsiU3ltYm9sVG9rZW4iOiJmQk5CIiwiVG9rZW5BZGRyIjoiMHg4N2IxQWNjRTZhMTk1OEU1MjIyMzNBNzM3MzEzQzA4NjU1MWE1Yzc2IiwiTHBBZGRyIjoiMHg3OGM2NzkzZGMxMDY1OWZlN2U0YWJhMTQwMmI5M2Y2ODljOGY0YzI3In1dfQ=="

But i want the base64 decoded version...

If I change the value b to be string when i insert it in cache like so:

for _, t := range listTokensFromDB {
        b, err := json.Marshal(t)
        if err != nil {
            fmt.Println(err)
        }

        ristrettoCache.Set(t.Symbol, string(b), 1)
    }

When i get the response, i get the stringified json like this: "{"Symbol":"bscCRYPTO","Addr":"0x2BcA1Ae3E52447323B..."

And i can't find a way to get out of this string :/

Anyone would know how i could get the real json please?

Thank you in advance and i wish u a good day!

CodePudding user response:

From my comments, I meant, in this line, value is most likely of type []byte (or []uint8 - which is the same thing)

value, found := ristrettoCache.Get(key)

JSON encoding a []byte will implicitly base64 the output - since JSON is text-based.

json.NewEncoder(w).Encode(value) // <- value is of type []byte

Inspecting the base64 you posted (https://play.golang.org/p/NAVS4qRfDM2) the underlying binary-bytes are already encoded in JSON - so no extra json.Encode is needed.

Just output the raw-bytes in your handler - and set the content-type to application/json:

func getTokenInfo(w http.ResponseWriter, r *http.Request){
    vars := mux.Vars(r)
    key := vars["chain"] vars["symbol"]
    value, found := ristrettoCache.Get(key)
    if !found {
        return
    }

    // json.NewEncoder(w).Encode(value) // not this

    w.Header().Set("Content-Type", "application/json")

    if bs, ok := value.([]byte); ok {
        _, err := w.Write(bs) //raw bytes (already encoded in JSON)

        // check 'err'
    } else {
        // error unexpected type behind interface{}
    }
}
  • Related