Created
November 15, 2018 07:17
-
-
Save joeke80215/d633a2d27e04cadf69e844ae9919185b to your computer and use it in GitHub Desktop.
Golang cookie cache struct in redis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"log" | |
"github.com/go-redis/redis" | |
) | |
type Cookie struct { | |
Data map[string]string | |
} | |
func main() { | |
client := redis.NewClient(&redis.Options{ | |
Addr: "<Redis host>", | |
Password: "<Redis password>", | |
DB: 0, // use default DB | |
}) | |
defer client.Close() | |
key := "sessions" | |
field := "ReqURL" | |
c := &Cookie{Data: map[string]string{"router": "routerToken", "sessID": "sessionToken"}} | |
cb, err := json.Marshal(c) | |
if err != nil { | |
log.Panic(err) | |
} | |
if _, err := client.HSet(key, field, cb).Result(); err != nil { | |
log.Panic(err) | |
} | |
cacheSess := client.HGet(key, field) | |
gCookie := &Cookie{} | |
if data, err := cacheSess.Result(); err != nil { | |
log.Panic(err) | |
} else { | |
err := json.Unmarshal([]byte(data), gCookie) | |
if err != nil { | |
log.Panic(err) | |
} | |
} | |
log.Println(*gCookie) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment