1
0
mirror of https://github.com/mxpv/podsync.git synced 2024-05-11 05:55:04 +00:00

Cache error feed responses

This commit is contained in:
Maksym Pavlenko
2019-05-18 01:02:19 -07:00
parent eb166c7d49
commit d4dac931c8

@ -159,8 +159,14 @@ func (s *Service) BuildFeed(hashID string) ([]byte, error) {
var (
logger = log.WithField("hash_id", hashID)
now = time.Now().UTC()
errKey = "err/" + hashID
)
cached, err := s.cache.Get(errKey)
if err == nil {
return []byte(cached), nil
}
feed, err := s.QueryFeed(hashID)
if err != nil {
logger.WithError(err).Error("failed to query feed from dynamodb")
@ -171,7 +177,10 @@ func (s *Service) BuildFeed(hashID string) ([]byte, error) {
oldLastID := feed.LastID
const updateTTL = 15 * time.Minute
const (
updateTTL = 15 * time.Minute
)
if now.Sub(feed.UpdatedAt) < updateTTL {
if podcast, err := s.buildPodcast(feed); err != nil {
return nil, err
@ -196,6 +205,9 @@ func (s *Service) BuildFeed(hashID string) ([]byte, error) {
if err := builder.Build(feed); err != nil {
logger.WithError(err).Error("failed to build feed")
// Save error to cache to avoid requests spamming
_ = s.cache.Set(errKey, err.Error(), updateTTL)
return nil, err
}