From ad67154e26e171587050d76a7efac97df58fe250 Mon Sep 17 00:00:00 2001 From: kolaente Date: Sun, 31 May 2020 17:59:39 +0200 Subject: [PATCH] Add cache for initial unsplash collection --- pkg/modules/background/unsplash/unsplash.go | 23 +++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pkg/modules/background/unsplash/unsplash.go b/pkg/modules/background/unsplash/unsplash.go index d0366010..babbc69d 100644 --- a/pkg/modules/background/unsplash/unsplash.go +++ b/pkg/modules/background/unsplash/unsplash.go @@ -28,6 +28,7 @@ import ( "net/http" "strconv" "strings" + "time" ) // Provider represents an unsplash image provider @@ -71,6 +72,15 @@ type Photo struct { // FIXME: Should use a proper cache var photos map[string]*Photo +// We're caching the initial collection to save a few api requests as this is retrieved every time a +// user opens the settings page. +type initialCollection struct { + lastCached time.Time + images []*background.Image +} + +var emptySearchResult *initialCollection + func init() { photos = make(map[string]*Photo) } @@ -119,6 +129,14 @@ func (p *Provider) Search(search string, page int64) (result []*background.Image // If we don't have a search query, return results from the unsplash featured collection if search == "" { + + if emptySearchResult != nil && time.Since(emptySearchResult.lastCached) < time.Minute { + log.Debugf("Serving intial unsplash collection from cache, last updated at %v", emptySearchResult.lastCached) + return emptySearchResult.images, nil + } + + log.Debug("Retrieving initial unsplash collection from unsplash api") + collectionResult := []*Photo{} err = doGet("collections/317099/photos?page="+strconv.FormatInt(page, 10)+"&per_page=25&order_by=latest", &collectionResult) if err != nil { @@ -138,6 +156,11 @@ func (p *Provider) Search(search string, page int64) (result []*background.Image }) photos[p.ID] = p } + + emptySearchResult = &initialCollection{ + lastCached: time.Now(), + images: result, + } return }