This file contains 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
const [fetchedData, setFetchedData] = useState([]); | |
useEffect(() => { | |
const getData = async () => { | |
const data = await axios.get( | |
"http://localhost:8081/users" | |
); | |
setFetchedData(data); | |
}; | |
getData(); | |
}, []); |
This file contains 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
topic := redisClient.Subscribe("send-user-name") | |
channel := topic.Channel() | |
for msg := range channel { | |
u := &User{} | |
// Unmarshal the data into the user | |
err := u.UnmarshalBinary([]byte(msg.Payload)) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Printf("User: %v having age: %v and id: %v\n", u.Name, u.Age, u.ID) |
This file contains 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
// Sample payload we are sending using pub/sub | |
{ | |
"id":"116c24b1-9425-4fe4-aec2-86ba7384733e", | |
"name":"Bob", | |
"age":29, | |
"source":"cache" | |
} | |
// Publish using Redis PubSub | |
payload, err := json.Marshal(resp) | |
if err := c.client.Publish("send-user-name", payload).Err(); err != nil { |
This file contains 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
router.HandleFunc("/users/{id}", func(w http.ResponseWriter, r *http.Request) { | |
id := mux.Vars(r)["id"] | |
val := redis.GetUser(id) | |
if val != nil { | |
val.Source = "cache" | |
renderJSON(w, &val, http.StatusOK) | |
return | |
} | |
user, err := GetUserByID(id) | |
if err != nil { |
This file contains 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
func (c *Client) GetUser(key string) (user *structs.Users) { | |
val, err := c.client.Get(key).Result() | |
resp := structs.Users{} | |
err = json.Unmarshal([]byte(val), &resp) | |
return &resp | |
} |
This file contains 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
func (c *Client) SetUser(key string, user structs.Users) { | |
json, err := json.Marshal(user) | |
if err != nil { | |
log.Fatal(err) | |
} | |
c.client.Set(key, json, 20*time.Second) | |
} |
This file contains 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
client := redis.NewClient(&redis.Options{ | |
Addr: "127.0.0.1:6379", | |
DB: 0, | |
DialTimeout: 100 * time.Millisecond, | |
ReadTimeout: 100 * time.Millisecond, | |
}) |
This file contains 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
type Users struct { | |
ID uuid.UUID `json:"id"` | |
Name string `json:"name"` | |
Age int `json:"age"` | |
CreatedTime string `json:"created_time"` | |
UpdatedTime string `json:"updated_time"` | |
Source string `json:"source"` | |
} |
This file contains 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
host := "127.0.0.1" | |
port := "5432" | |
user := "user" | |
password := "password" | |
dbname := "dbname" | |
psqlInfo := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname) | |
result, err := sql.Open("postgres", psqlInfo) | |
if err != nil { | |
log.Fatalf("Error in connection : %s", err) | |
} |
This file contains 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
srv := &http.Server{ | |
Handler: router, | |
Addr: ":8081", | |
WriteTimeout: 15 * time.Second, | |
ReadTimeout: 15 * time.Second, | |
} | |
log.Fatal(srv.ListenAndServe()) |