Skip to content

TuHo_Week2-DB-Redis#18

Open
hoanhtu wants to merge 1 commit into
EngineerProOrg:scott/sample-gormfrom
hoanhtu:scott/sample-gorm
Open

TuHo_Week2-DB-Redis#18
hoanhtu wants to merge 1 commit into
EngineerProOrg:scott/sample-gormfrom
hoanhtu:scott/sample-gorm

Conversation

@hoanhtu

@hoanhtu hoanhtu commented Jun 3, 2023

Copy link
Copy Markdown

No description provided.

@hoanhtu hoanhtu changed the title TuHo_Week2-Gin_Database_Redis TuHo_Week2-DB-Redis Jun 3, 2023
Comment thread cmd/home_work/main.go
}

// Tạo session id ngẫu nhiên
rand.Seed(time.Now().UnixNano())

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

random like this can make collision rate high if number of user is big , you should use uuid, or hash from user_name

Comment thread cmd/home_work/main.go

value, err := redisClient.Get(redisClient.Context(), sessionID).Result()
if err != nil {
c.AbortWithStatusJSON(429, gin.H{"error": "cant get data user"})

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return here ?

Comment thread cmd/home_work/main.go

if errPing != nil || now-timeCount > 60 {
myMap := map[string]interface{}{
"timeCount": now,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to implement like this, only need to store the count number (no need time) then set an expire time for it (60s in this case) then if it over 60s, this key will be deleted (which mean if someone increase or get will be 0)

Comment thread cmd/home_work/main.go
Comment thread cmd/home_work/main.go
}

// Truy vấn số lượng request của người dùng đó từ Redis
count, err := redisClient.Get(redisClient.Context(), keyUser).Int()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can get the count number from the response of incr command instead, no need to call one more time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants