Skip to content

Commit

Permalink
adds subscriptions cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Seklfreak committed Mar 7, 2020
1 parent 33eb8eb commit 15afc54
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 27 deletions.
55 changes: 55 additions & 0 deletions pkg/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package pkg

import (
"sync"
"time"

"google.golang.org/api/youtube/v3"
)

const expiry = 24 * time.Hour

type item struct {
Updated time.Time
Subs []*youtube.Subscription
}

var (
subsCache = make(map[string]*item)
subsCacheLock sync.RWMutex
)

func readSubsCache(key string) []*youtube.Subscription {
subsCacheLock.RLock()
defer subsCacheLock.RUnlock()

item := subsCache[key]
if item == nil {
return nil
}

if !item.Updated.After(time.Now().Add(-expiry)) {
return nil
}

subsCopy := make([]*youtube.Subscription, len(item.Subs))
copy(subsCopy, item.Subs)
return subsCopy
}

func storeSubsCache(key string, subs []*youtube.Subscription) {
subsCacheLock.Lock()
defer subsCacheLock.Unlock()

if subs == nil {
subsCache[key] = nil
return
}

subsCopy := make([]*youtube.Subscription, len(subs))
copy(subsCopy, subs)
subsCache[key] = &item{
Updated: time.Now(),
Subs: subsCopy,
}
}
56 changes: 29 additions & 27 deletions pkg/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/http"

"go.uber.org/zap"
"google.golang.org/api/youtube/v3"
)

const (
Expand All @@ -31,39 +30,42 @@ func ExportHandler(w http.ResponseWriter, r *http.Request) {
return
}

yt, err := ytServiceFromRefreshToken(r.Context(), refreshToken)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}

var items []*youtube.Subscription

var pageToken string
for {
logger.Info("making YouTube API request", zap.String("page_token", pageToken))

resp, err := yt.Subscriptions.
List("snippet").
MaxResults(50).
Order("alphabetical").
Mine(true).
PageToken(pageToken).
Do()
items := readSubsCache(refreshToken)
if len(items) <= 0 {
yt, err := ytServiceFromRefreshToken(r.Context(), refreshToken)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}

items = append(items, resp.Items...)
var pageToken string
for {
logger.Info("making YouTube API request", zap.String("page_token", pageToken))

pageToken = resp.NextPageToken
if resp.NextPageToken == "" {
break
resp, err := yt.Subscriptions.
List("snippet").
MaxResults(50).
Order("alphabetical").
Mine(true).
PageToken(pageToken).
Do()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

items = append(items, resp.Items...)

pageToken = resp.NextPageToken
if resp.NextPageToken == "" {
break
}
}
}

logger.Info("found items", zap.Int("amount", len(items)))
logger.Info("received item from API", zap.Int("amount", len(items)))

storeSubsCache(refreshToken, items)
}

var result string
for _, item := range items {
Expand Down

0 comments on commit 15afc54

Please sign in to comment.