Add caching to folders API #260

Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
Michael Mayer 2020-05-22 20:00:33 +02:00
parent c88cd69e5c
commit 75fd1904f3
5 changed files with 44 additions and 3 deletions

View File

@ -15,6 +15,7 @@ import (
"github.com/photoprism/photoprism/internal/event"
"github.com/photoprism/photoprism/internal/form"
"github.com/photoprism/photoprism/internal/query"
"github.com/photoprism/photoprism/internal/service"
"github.com/photoprism/photoprism/internal/thumb"
"github.com/photoprism/photoprism/pkg/fs"
"github.com/photoprism/photoprism/pkg/rnd"
@ -429,7 +430,7 @@ func AlbumThumbnail(router *gin.RouterGroup, conf *config.Config) {
return
}
gc := conf.Cache()
gc := service.Cache()
cacheKey := fmt.Sprintf("album-thumbnail:%s:%s", uuid, typeName)
if cacheData, ok := gc.Get(cacheKey); ok {

View File

@ -1,11 +1,14 @@
package api
import (
"fmt"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/service"
)
// GetFolders is a reusable request handler for directory listings (GET /api/v1/folders/*).
@ -16,12 +19,26 @@ func GetFolders(router *gin.RouterGroup, conf *config.Config, root, pathName str
return
}
start := time.Now()
gc := service.Cache()
recursive := c.Query("recursive") != ""
cacheKey := fmt.Sprintf("folders:%s:%t", pathName, recursive)
if cacheData, ok := gc.Get(cacheKey); ok {
log.Debugf("folders: %s cache hit [%s]", cacheKey, time.Since(start))
c.JSON(http.StatusOK, cacheData.([]entity.Folder))
return
}
folders, err := entity.Folders(root, pathName, recursive)
if err != nil {
log.Errorf("folder: %s", err)
log.Errorf("folders: %s", err)
} else {
gc.Set(cacheKey, folders, time.Minute*5)
log.Debugf("folders: %s cached [%s]", cacheKey, time.Since(start))
}
c.JSON(http.StatusOK, folders)

View File

@ -15,6 +15,7 @@ import (
"github.com/photoprism/photoprism/internal/event"
"github.com/photoprism/photoprism/internal/form"
"github.com/photoprism/photoprism/internal/query"
"github.com/photoprism/photoprism/internal/service"
"github.com/photoprism/photoprism/internal/thumb"
"github.com/photoprism/photoprism/pkg/fs"
"github.com/photoprism/photoprism/pkg/txt"
@ -175,7 +176,7 @@ func LabelThumbnail(router *gin.RouterGroup, conf *config.Config) {
return
}
gc := conf.Cache()
gc := service.Cache()
cacheKey := fmt.Sprintf("label-thumbnail:%s:%s", labelUUID, typeName)
if cacheData, ok := gc.Get(cacheKey); ok {

20
internal/service/cache.go Normal file
View File

@ -0,0 +1,20 @@
package service
import (
"sync"
"time"
gc "github.com/patrickmn/go-cache"
)
var onceCache sync.Once
func initCache() {
services.Cache = gc.New(336*time.Hour, 30*time.Minute)
}
func Cache() *gc.Cache {
onceCache.Do(initCache)
return services.Cache
}

View File

@ -1,6 +1,7 @@
package service
import (
gc "github.com/patrickmn/go-cache"
"github.com/photoprism/photoprism/internal/classify"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/nsfw"
@ -12,6 +13,7 @@ import (
var conf *config.Config
var services struct {
Cache *gc.Cache
Classify *classify.TensorFlow
Convert *photoprism.Convert
Import *photoprism.Import