feat: use keyvalue.Remember where it makes sense

This commit is contained in:
kolaente
2025-07-17 15:49:58 +02:00
parent c7a98386c2
commit fcdcdcf46a
6 changed files with 79 additions and 136 deletions

View File

@@ -122,22 +122,16 @@ func getImageID(fullURL string) string {
// Gets an unsplash photo either from cache or directly from the unsplash api
func getUnsplashPhotoInfoByID(photoID string) (photo *Photo, err error) {
photo = &Photo{}
exists, err := keyvalue.GetWithValue(cachePrefix+photoID, photo)
result, err := keyvalue.Remember(cachePrefix+photoID, func() (any, error) {
log.Debugf("Image information for unsplash photo %s not cached, requesting from unsplash...", photoID)
photo := &Photo{}
err := doGet("photos/"+photoID, photo)
return photo, err
})
if err != nil {
return nil, err
}
if !exists {
log.Debugf("Image information for unsplash photo %s not cached, requesting from unsplash...", photoID)
photo = &Photo{}
err = doGet("photos/"+photoID, photo)
if err != nil {
return
}
}
return
return result.(*Photo), nil
}
// Search is the implementation to search on unsplash