Skip to content

Commit

Permalink
Add random number to prevent collision
Browse files Browse the repository at this point in the history
  • Loading branch information
Wikidepia committed Mar 8, 2024
1 parent eb7ad06 commit 340585f
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion handlers/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"instafix/utils"
"math/rand"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -129,7 +130,8 @@ func (i *InstaData) GetData(postID string) error {

// Write expire to DB
expire := make([]byte, 8)
binary.LittleEndian.PutUint64(expire, uint64(time.Now().Add(24*time.Hour).UnixNano()))
randNum := rand.Intn(9999-1000) + 1000
binary.LittleEndian.PutUint64(expire, uint64(time.Now().Add(24*time.Hour).UnixNano()+int64(randNum)))
if err := batch.Set(append([]byte("exp-"), expire...), utils.S2B(postID), pebble.Sync); err != nil {
log.Error().Str("postID", postID).Err(err).Msg("Failed to save data to cache")
return err
Expand Down

0 comments on commit 340585f

Please sign in to comment.