Skip to content

Commit

Permalink
Put caching inside singleflight
Browse files Browse the repository at this point in the history
  • Loading branch information
Wikidepia committed Jul 2, 2024
1 parent 8ba0c0d commit 2d77843
Showing 1 changed file with 35 additions and 35 deletions.
70 changes: 35 additions & 35 deletions handlers/scraper/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,48 +77,48 @@ func (i *InstaData) GetData() error {
} else {
log.Warn().Str("postID", i.PostID).Err(err).Msg("Post not found")
}
return nil, err
return false, err
}
return true, nil
})
if err != nil {
return err
}

// Replace all media urls cdn to scontent.cdninstagram.com
for n, media := range i.Medias {
u, err := url.Parse(media.URL)
if err != nil {
log.Error().Str("postID", i.PostID).Err(err).Msg("Failed to parse media URL")
return err
// Replace all media urls cdn to scontent.cdninstagram.com
for n, media := range i.Medias {
u, err := url.Parse(media.URL)
if err != nil {
log.Error().Str("postID", i.PostID).Err(err).Msg("Failed to parse media URL")
return false, err
}
u.Host = "scontent.cdninstagram.com"
i.Medias[n].URL = u.String()
}
u.Host = "scontent.cdninstagram.com"
i.Medias[n].URL = u.String()
}

bb, err := binary.Marshal(i)
if err != nil {
log.Error().Str("postID", i.PostID).Err(err).Msg("Failed to marshal data")
return err
}
bb, err := binary.Marshal(i)
if err != nil {
log.Error().Str("postID", i.PostID).Err(err).Msg("Failed to marshal data")
return false, err
}

batch := DB.NewBatch()
// Write cache to DB
if err := batch.Set(utils.S2B(i.PostID), bb, pebble.Sync); err != nil {
log.Error().Str("postID", i.PostID).Err(err).Msg("Failed to save data to cache")
return err
}
batch := DB.NewBatch()
// Write cache to DB
if err := batch.Set(utils.S2B(i.PostID), bb, pebble.Sync); err != nil {
log.Error().Str("postID", i.PostID).Err(err).Msg("Failed to save data to cache")
return false, err
}

// Write expire to DB
expTime := strconv.FormatInt(time.Now().Add(24*time.Hour).UnixNano(), 10)
if err := batch.Set(append([]byte("exp-"), expTime...), utils.S2B(i.PostID), pebble.Sync); err != nil {
log.Error().Str("postID", i.PostID).Err(err).Msg("Failed to save data to cache")
return err
}
// Write expire to DB
expTime := strconv.FormatInt(time.Now().Add(24*time.Hour).UnixNano(), 10)
if err := batch.Set(append([]byte("exp-"), expTime...), utils.S2B(i.PostID), pebble.Sync); err != nil {
log.Error().Str("postID", i.PostID).Err(err).Msg("Failed to save data to cache")
return false, err
}

// Commit batch
if err := batch.Commit(pebble.Sync); err != nil {
log.Error().Str("postID", i.PostID).Err(err).Msg("Failed to commit batch")
// Commit batch
if err := batch.Commit(pebble.Sync); err != nil {
log.Error().Str("postID", i.PostID).Err(err).Msg("Failed to commit batch")
return false, err
}
return true, nil
})
if err != nil {
return err
}
return nil
Expand Down

0 comments on commit 2d77843

Please sign in to comment.