Skip to content

Instantly share code, notes, and snippets.

View AseemWangoo's full-sized avatar
🐝
Beeing

Aseem Wangoo AseemWangoo

🐝
Beeing
View GitHub Profile
@AseemWangoo
AseemWangoo / users.go
Created October 19, 2022 15:11
user struct
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"`
}
@AseemWangoo
AseemWangoo / redis_client.go
Created October 19, 2022 15:12
connect to redis
client := redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379",
DB: 0,
DialTimeout: 100 * time.Millisecond,
ReadTimeout: 100 * time.Millisecond,
})
@AseemWangoo
AseemWangoo / set_redis.go
Created October 19, 2022 15:13
Setting user in redis
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)
}
@AseemWangoo
AseemWangoo / get_user.go
Created October 19, 2022 15:14
Get user in redis
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
}
@AseemWangoo
AseemWangoo / user_apis.go
Created October 19, 2022 15:14
User apis
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 {
@AseemWangoo
AseemWangoo / redis_publisher.go
Created October 19, 2022 15:15
Publish using redis
// 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 {
@AseemWangoo
AseemWangoo / redis_subscriber.go
Created October 19, 2022 15:16
Subscribe using redis
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)
@AseemWangoo
AseemWangoo / caching.js
Created October 19, 2022 15:16
Caching in react
const [fetchedData, setFetchedData] = useState([]);
useEffect(() => {
const getData = async () => {
const data = await axios.get(
"http://localhost:8081/users"
);
setFetchedData(data);
};
getData();
}, []);
@AseemWangoo
AseemWangoo / install.sh
Created December 20, 2022 06:16
Install llvm
brew install llvm
@AseemWangoo
AseemWangoo / create.sh
Created December 20, 2022 06:17
Create dart project
dart create ffi_2_18
## ffi_2_18 is the name of the project which will be created