@@ -32,6 +32,27 @@ const (
3232
3333var (
3434 albums []models.BandcampAlbumData
35+ // Add a cache for home page stats
36+ homePageCache struct {
37+ Albums []models.BandcampAlbumData
38+ TotalAlbums int
39+ TotalSongs int
40+ TotalWords int
41+ TotalChars int
42+ TotalCharsNoSpaces int
43+ TotalVowels int
44+ TotalConsonants int
45+ TotalLines int
46+ TotalDuration int
47+ AvgWordsPerAlbum int
48+ AvgCharsPerAlbum int
49+ AvgWordLength float64
50+ AvgSongsPerAlbum float64
51+ WPM int
52+ ProjectedAlbums float64
53+ FuckCount int
54+ DisplayAlbums []models.BandcampAlbumData
55+ }
3556)
3657
3758type TemplateRenderer struct {
@@ -57,6 +78,9 @@ func main() {
5778 if err := loadAlbums (); err != nil {
5879 e .Logger .Fatal (err )
5980 }
81+ if err := refreshHomePageCache (); err != nil {
82+ e .Logger .Fatal (err )
83+ }
6084
6185 setupRoutes (e )
6286 setupAdminRoutes (e , renderer )
@@ -158,7 +182,7 @@ func countWordOccurrences(albums []models.BandcampAlbumData, word string) int {
158182 return count
159183}
160184
161- func indexHandler ( c echo. Context ) error {
185+ func refreshHomePageCache ( ) error {
162186 allAlbums , err := loader .LoadAlbumsData ()
163187 if err != nil {
164188 return err
@@ -215,24 +239,67 @@ func indexHandler(c echo.Context) error {
215239
216240 fuckCount := countWordOccurrences (allAlbums , "fuck" )
217241
242+ homePageCache = struct {
243+ Albums []models.BandcampAlbumData
244+ TotalAlbums int
245+ TotalSongs int
246+ TotalWords int
247+ TotalChars int
248+ TotalCharsNoSpaces int
249+ TotalVowels int
250+ TotalConsonants int
251+ TotalLines int
252+ TotalDuration int
253+ AvgWordsPerAlbum int
254+ AvgCharsPerAlbum int
255+ AvgWordLength float64
256+ AvgSongsPerAlbum float64
257+ WPM int
258+ ProjectedAlbums float64
259+ FuckCount int
260+ DisplayAlbums []models.BandcampAlbumData
261+ }{
262+ Albums : allAlbums ,
263+ TotalAlbums : albumCount ,
264+ TotalSongs : totalSongs ,
265+ TotalWords : totalWords ,
266+ TotalChars : totalChars ,
267+ TotalCharsNoSpaces : totalCharsNoSpaces ,
268+ TotalVowels : totalVowels ,
269+ TotalConsonants : totalConsonants ,
270+ TotalLines : totalLines ,
271+ TotalDuration : totalDuration / 60 ,
272+ AvgWordsPerAlbum : avgWordsPerAlbum ,
273+ AvgCharsPerAlbum : totalChars / max (albumCount , 1 ),
274+ AvgWordLength : avgWordLength ,
275+ AvgSongsPerAlbum : avgSongsPerAlbum ,
276+ WPM : wpm ,
277+ ProjectedAlbums : projectedAlbums ,
278+ FuckCount : fuckCount ,
279+ DisplayAlbums : displayAlbums ,
280+ }
281+ return nil
282+ }
283+
284+ func indexHandler (c echo.Context ) error {
218285 return renderTemplate (c , "index.html" , map [string ]interface {}{
219- "albums" : displayAlbums ,
220- "TotalAlbums" : albumCount ,
221- "TotalSongs" : totalSongs ,
222- "TotalWords" : totalWords ,
223- "TotalChars" : totalChars ,
224- "TotalCharsNoSpaces" : totalCharsNoSpaces ,
225- "TotalVowels" : totalVowels ,
226- "TotalConsonants" : totalConsonants ,
227- "TotalLines" : totalLines ,
228- "TotalDuration" : totalDuration / 60 ,
229- "AvgWordsPerAlbum" : avgWordsPerAlbum ,
230- "AvgCharsPerAlbum" : totalChars / max ( albumCount , 1 ) ,
231- "AvgWordLength" : avgWordLength ,
232- "AvgSongsPerAlbum" : avgSongsPerAlbum ,
233- "WPM" : wpm ,
234- "ProjectedAlbums" : projectedAlbums ,
235- "FuckCount" : fuckCount ,
286+ "albums" : homePageCache . DisplayAlbums ,
287+ "TotalAlbums" : homePageCache . TotalAlbums ,
288+ "TotalSongs" : homePageCache . TotalSongs ,
289+ "TotalWords" : homePageCache . TotalWords ,
290+ "TotalChars" : homePageCache . TotalChars ,
291+ "TotalCharsNoSpaces" : homePageCache . TotalCharsNoSpaces ,
292+ "TotalVowels" : homePageCache . TotalVowels ,
293+ "TotalConsonants" : homePageCache . TotalConsonants ,
294+ "TotalLines" : homePageCache . TotalLines ,
295+ "TotalDuration" : homePageCache . TotalDuration ,
296+ "AvgWordsPerAlbum" : homePageCache . AvgWordsPerAlbum ,
297+ "AvgCharsPerAlbum" : homePageCache . AvgCharsPerAlbum ,
298+ "AvgWordLength" : homePageCache . AvgWordLength ,
299+ "AvgSongsPerAlbum" : homePageCache . AvgSongsPerAlbum ,
300+ "WPM" : homePageCache . WPM ,
301+ "ProjectedAlbums" : homePageCache . ProjectedAlbums ,
302+ "FuckCount" : homePageCache . FuckCount ,
236303 })
237304}
238305
@@ -541,6 +608,9 @@ func importAlbumHandler(c echo.Context) error {
541608 if err := loadAlbums (); err != nil {
542609 log .Printf ("Error reloading albums after import: %v" , err )
543610 }
611+ if err := refreshHomePageCache (); err != nil {
612+ log .Printf ("Error refreshing home page cache after import: %v" , err )
613+ }
544614
545615 log .Printf ("Import complete" )
546616 return c .HTML (http .StatusOK , strings .Join (results , "\n " ))
0 commit comments