1
0

Rewrote LastSong handler using caching IcecastLastSong function. And IcecastLastPlayedSongs is using cache as well. Now it takes between 300 nanoseconds to 2 microseconds to get a list of last songs.

This commit is contained in:
Alexander Andreev 2022-03-31 15:39:51 +04:00
parent 3b51fffa60
commit 70580298d2
Signed by: Arav
GPG Key ID: 1327FE8A374CC86F

View File

@ -113,22 +113,22 @@ func (h *RadioHandlers) Stats(w http.ResponseWriter, r *http.Request) {
} }
func (h *RadioHandlers) LastSong(w http.ResponseWriter, r *http.Request) { func (h *RadioHandlers) LastSong(w http.ResponseWriter, r *http.Request) {
songs, err := radio.IcecastLastPlayedSongs(1, h.conf.Icecast.Playlist) song, err := radio.IcecastLastSong(h.conf.Icecast.Playlist)
if err != nil { if err != nil {
h.logErr.Println("cannot retrieve last songs:", err) h.logErr.Println("cannot retrieve last songs:", err)
} }
if len(songs) == 0 { if song.Artist == "" {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
return return
} }
if tim, err := time.Parse("15:04-0700", songs[0].Time); err == nil { if tim, err := time.Parse("15:04-0700", song.Time); err == nil {
songs[0].Time = utils.ToClientTimezone(tim, r).Format("15:04") song.Time = utils.ToClientTimezone(tim, r).Format("15:04")
} }
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(songs[0]) json.NewEncoder(w).Encode(song)
} }
func (h *RadioHandlers) Playlist(w http.ResponseWriter, _ *http.Request) { func (h *RadioHandlers) Playlist(w http.ResponseWriter, _ *http.Request) {