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
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
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
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
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
// 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
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
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
brew install llvm |
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
dart create ffi_2_18 | |
## ffi_2_18 is the name of the project which will be created |