Lock the cache check, so we don't refresh it three times for every singer
Signed-off-by: Martyn Ranyard <m@rtyn.berlin>
This commit is contained in:
parent
3966e6e226
commit
a8dbd68031
|
@ -6,6 +6,7 @@ import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
|
"sync"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
irc "git.martyn.berlin/martyn/twitchsingstools/internal/irc"
|
irc "git.martyn.berlin/martyn/twitchsingstools/internal/irc"
|
||||||
|
@ -235,22 +236,8 @@ func AdminHandler(response http.ResponseWriter, request *http.Request) {
|
||||||
TopNSingers []SingerSings
|
TopNSingers []SingerSings
|
||||||
ChannelKey string
|
ChannelKey string
|
||||||
}
|
}
|
||||||
|
updateCacheIfNecessary(vars["channel"])
|
||||||
channelData := ircBot.ChannelData[vars["channel"]]
|
channelData := ircBot.ChannelData[vars["channel"]]
|
||||||
if time.Now().Sub(ircBot.ChannelData[vars["channel"]].VideoCacheUpdated).Hours() > 1 {
|
|
||||||
fmt.Printf("Cache of %d performances is older than an hour - %.1f hours old to be precise... fetching.\n", len(ircBot.ChannelData[vars["channel"]].VideoCache), time.Now().Sub(ircBot.ChannelData[vars["channel"]].VideoCacheUpdated).Hours())
|
|
||||||
vids, err := fetchAllVoDs(channelData.TwitchUserID, channelData.Bearer)
|
|
||||||
if err != nil {
|
|
||||||
errCache := make([]irc.SingsVideoStruct, 0)
|
|
||||||
var ret irc.SingsVideoStruct
|
|
||||||
ret.FullTitle = "Error fetching videos: " + err.Error()
|
|
||||||
errCache = append(errCache, ret)
|
|
||||||
vids = errCache
|
|
||||||
}
|
|
||||||
updateCalculatedFields(vids)
|
|
||||||
ircBot.UpdateVideoCache(vars["channel"], vids)
|
|
||||||
} else {
|
|
||||||
fmt.Printf("Cache of %d performances is younger than an hour - %.1f hours old to be precise... not fetching.\n", len(ircBot.ChannelData[vars["channel"]].VideoCache), time.Now().Sub(ircBot.ChannelData[vars["channel"]].VideoCacheUpdated).Hours())
|
|
||||||
}
|
|
||||||
updateCalculatedFields(channelData.VideoCache)
|
updateCalculatedFields(channelData.VideoCache)
|
||||||
for _, song := range channelData.VideoCache {
|
for _, song := range channelData.VideoCache {
|
||||||
if song.Duet && song.OtherSinger == "" {
|
if song.Duet && song.OtherSinger == "" {
|
||||||
|
@ -490,6 +477,29 @@ func (h *KVHeap) Pop() interface{} {
|
||||||
return x
|
return x
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var cacheLock sync.Mutex
|
||||||
|
|
||||||
|
func updateCacheIfNecessary(channel string) {
|
||||||
|
cacheLock.Lock()
|
||||||
|
channelData := ircBot.ChannelData[channel]
|
||||||
|
if time.Now().Sub(channelData.VideoCacheUpdated).Hours() > 1 {
|
||||||
|
fmt.Printf("Cache of %d performances is older than an hour - %.1f hours old to be precise... fetching.\n", len(channelData.VideoCache), time.Now().Sub(ircBot.ChannelData[channel].VideoCacheUpdated).Hours())
|
||||||
|
vids, err := fetchAllVoDs(channelData.TwitchUserID, channelData.Bearer)
|
||||||
|
if err != nil {
|
||||||
|
errCache := make([]irc.SingsVideoStruct, 0)
|
||||||
|
var ret irc.SingsVideoStruct
|
||||||
|
ret.FullTitle = "Error fetching videos: " + err.Error()
|
||||||
|
errCache = append(errCache, ret)
|
||||||
|
vids = errCache
|
||||||
|
}
|
||||||
|
updateCalculatedFields(vids)
|
||||||
|
ircBot.UpdateVideoCache(channel, vids)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Cache of %d performances is younger than an hour - %.1f hours old to be precise... not fetching.\n", len(channelData.VideoCache), time.Now().Sub(ircBot.ChannelData[channel].VideoCacheUpdated).Hours())
|
||||||
|
}
|
||||||
|
cacheLock.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
func calculateTopNSingers(songCache []irc.SingsVideoStruct, howMany int) []SingerSings {
|
func calculateTopNSingers(songCache []irc.SingsVideoStruct, howMany int) []SingerSings {
|
||||||
songMap := map[string]int{}
|
songMap := map[string]int{}
|
||||||
songCount := 0
|
songCount := 0
|
||||||
|
@ -746,22 +756,8 @@ func CSVHandler(response http.ResponseWriter, request *http.Request) {
|
||||||
TopNSongs []SongSings
|
TopNSongs []SongSings
|
||||||
TopNSingers []SingerSings
|
TopNSingers []SingerSings
|
||||||
}
|
}
|
||||||
|
updateCacheIfNecessary(vars["channel"])
|
||||||
channelData := ircBot.ChannelData[vars["channel"]]
|
channelData := ircBot.ChannelData[vars["channel"]]
|
||||||
if time.Now().Sub(ircBot.ChannelData[vars["channel"]].VideoCacheUpdated).Hours() > 1 {
|
|
||||||
fmt.Printf("Cache of %d performances is older than an hour - %.1f hours old to be precise... fetching.\n", len(ircBot.ChannelData[vars["channel"]].VideoCache), time.Now().Sub(ircBot.ChannelData[vars["channel"]].VideoCacheUpdated).Hours())
|
|
||||||
vids, err := fetchAllVoDs(channelData.TwitchUserID, channelData.Bearer)
|
|
||||||
if err != nil {
|
|
||||||
errCache := make([]irc.SingsVideoStruct, 0)
|
|
||||||
var ret irc.SingsVideoStruct
|
|
||||||
ret.FullTitle = "Error fetching videos: " + err.Error()
|
|
||||||
errCache = append(errCache, ret)
|
|
||||||
vids = errCache
|
|
||||||
}
|
|
||||||
updateCalculatedFields(vids)
|
|
||||||
ircBot.UpdateVideoCache(vars["channel"], vids)
|
|
||||||
} else {
|
|
||||||
fmt.Printf("Cache of %d performances is younger than an hour - %.1f hours old to be precise... not fetching.\n", len(ircBot.ChannelData[vars["channel"]].VideoCache), time.Now().Sub(ircBot.ChannelData[vars["channel"]].VideoCacheUpdated).Hours())
|
|
||||||
}
|
|
||||||
topNSongs := calculateTopNSongs(channelData.VideoCache, 10)
|
topNSongs := calculateTopNSongs(channelData.VideoCache, 10)
|
||||||
topNSingers := calculateTopNSingers(channelData.VideoCache, 10)
|
topNSingers := calculateTopNSingers(channelData.VideoCache, 10)
|
||||||
var td = TemplateData{channelData.Name, channelData.Command, channelData.ExtraStrings, channelData.JoinTime, channelData.JoinTime.Format(irc.UTCFormat), false, channelData.HasLeft, AugmentSingsVideoStructSliceForCSV(channelData.VideoCache), topNSongs, topNSingers}
|
var td = TemplateData{channelData.Name, channelData.Command, channelData.ExtraStrings, channelData.JoinTime, channelData.JoinTime.Format(irc.UTCFormat), false, channelData.HasLeft, AugmentSingsVideoStructSliceForCSV(channelData.VideoCache), topNSongs, topNSingers}
|
||||||
|
@ -796,22 +792,8 @@ func JSONHandler(response http.ResponseWriter, request *http.Request) {
|
||||||
TopNSongs []SongSings
|
TopNSongs []SongSings
|
||||||
TopNSingers []SingerSings
|
TopNSingers []SingerSings
|
||||||
}
|
}
|
||||||
|
updateCacheIfNecessary(vars["channel"])
|
||||||
channelData := ircBot.ChannelData[vars["channel"]]
|
channelData := ircBot.ChannelData[vars["channel"]]
|
||||||
if time.Now().Sub(ircBot.ChannelData[vars["channel"]].VideoCacheUpdated).Hours() > 1 {
|
|
||||||
fmt.Printf("Cache of %d performances is older than an hour - %.1f hours old to be precise... fetching.\n", len(ircBot.ChannelData[vars["channel"]].VideoCache), time.Now().Sub(ircBot.ChannelData[vars["channel"]].VideoCacheUpdated).Hours())
|
|
||||||
vids, err := fetchAllVoDs(channelData.TwitchUserID, channelData.Bearer)
|
|
||||||
if err != nil {
|
|
||||||
errCache := make([]irc.SingsVideoStruct, 0)
|
|
||||||
var ret irc.SingsVideoStruct
|
|
||||||
ret.FullTitle = "Error fetching videos: " + err.Error()
|
|
||||||
errCache = append(errCache, ret)
|
|
||||||
vids = errCache
|
|
||||||
}
|
|
||||||
updateCalculatedFields(vids)
|
|
||||||
ircBot.UpdateVideoCache(vars["channel"], vids)
|
|
||||||
} else {
|
|
||||||
fmt.Printf("Cache of %d performances is younger than an hour - %.1f hours old to be precise... not fetching.\n", len(ircBot.ChannelData[vars["channel"]].VideoCache), time.Now().Sub(ircBot.ChannelData[vars["channel"]].VideoCacheUpdated).Hours())
|
|
||||||
}
|
|
||||||
topNSongs := calculateTopNSongs(channelData.VideoCache, 10)
|
topNSongs := calculateTopNSongs(channelData.VideoCache, 10)
|
||||||
topNSingers := calculateTopNSingers(channelData.VideoCache, 10)
|
topNSingers := calculateTopNSingers(channelData.VideoCache, 10)
|
||||||
var td = TemplateData{channelData.Name, channelData.Command, channelData.ExtraStrings, channelData.JoinTime, channelData.JoinTime.Format(irc.UTCFormat), false, channelData.HasLeft, AugmentSingsVideoStructSlice(channelData.VideoCache), topNSongs, topNSingers}
|
var td = TemplateData{channelData.Name, channelData.Command, channelData.ExtraStrings, channelData.JoinTime, channelData.JoinTime.Format(irc.UTCFormat), false, channelData.HasLeft, AugmentSingsVideoStructSlice(channelData.VideoCache), topNSongs, topNSingers}
|
||||||
|
|
Loading…
Reference in New Issue