Cache in batch metadata api

This commit is contained in:
LittleSheep 2024-08-06 23:45:58 +08:00
parent 4c5d6d5ec5
commit 13893e87f7
2 changed files with 44 additions and 5 deletions

View File

@ -1,10 +1,12 @@
package api package api
import ( import (
"strconv"
"strings" "strings"
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database" "git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/models" "git.solsynth.dev/hydrogen/paperclip/pkg/internal/models"
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/services"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
@ -18,8 +20,26 @@ func listAttachment(c *fiber.Ctx) error {
tx := database.C tx := database.C
var result = make([]models.Attachment, take)
var idxList []uint
if len(c.Query("id")) > 0 { if len(c.Query("id")) > 0 {
tx = tx.Where("id IN ?", strings.Split(c.Query("id"), ",")) var pendingQueryId []uint
idx := strings.Split(c.Query("id"), ",")
for p, raw := range idx {
id, err := strconv.Atoi(raw)
if err != nil {
continue
} else {
idxList = append(idxList, uint(id))
}
if val, ok := services.GetAttachmentCache(uint(id)); ok {
result[p] = val
} else {
pendingQueryId = append(pendingQueryId, uint(id))
}
}
tx = tx.Where("id IN ?", pendingQueryId)
} else { } else {
// Do sort this when doesn't filter by the id // Do sort this when doesn't filter by the id
// Because the sort will mess up the result // Because the sort will mess up the result
@ -44,13 +64,25 @@ func listAttachment(c *fiber.Ctx) error {
if err := countTx.Model(&models.Attachment{}).Count(&count).Error; err != nil { if err := countTx.Model(&models.Attachment{}).Count(&count).Error; err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error()) return fiber.NewError(fiber.StatusBadRequest, err.Error())
} }
var attachments []models.Attachment var out []models.Attachment
if err := tx.Offset(offset).Limit(take).Preload("Account").Find(&attachments).Error; err != nil { if err := tx.Offset(offset).Limit(take).Preload("Account").Find(&out).Error; err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error()) return fiber.NewError(fiber.StatusBadRequest, err.Error())
} }
if len(idxList) == 0 {
result = out
} else {
for _, item := range out {
for p, id := range idxList {
if item.ID == id {
result[p] = item
}
}
}
}
return c.JSON(fiber.Map{ return c.JSON(fiber.Map{
"count": count, "count": count,
"data": attachments, "data": result,
}) })
} }

View File

@ -21,7 +21,7 @@ const metadataCacheLimit = 512
var metadataCache sync.Map var metadataCache sync.Map
func GetAttachmentByID(id uint) (models.Attachment, error) { func GetAttachmentByID(id uint) (models.Attachment, error) {
if val, ok := metadataCache.Load(id); ok && val.(models.Attachment).AccountID > 0 { if val, ok := metadataCache.Load(id); ok && val.(models.Attachment).Account.ID > 0 {
return val.(models.Attachment), nil return val.(models.Attachment), nil
} }
@ -48,6 +48,13 @@ func GetAttachmentByHash(hash string) (models.Attachment, error) {
return attachment, nil return attachment, nil
} }
func GetAttachmentCache(id uint) (models.Attachment, bool) {
if val, ok := metadataCache.Load(id); ok && val.(models.Attachment).Account.ID > 0 {
return val.(models.Attachment), ok
}
return models.Attachment{}, false
}
func NewAttachmentMetadata(tx *gorm.DB, user models.Account, file *multipart.FileHeader, attachment models.Attachment) (models.Attachment, error) { func NewAttachmentMetadata(tx *gorm.DB, user models.Account, file *multipart.FileHeader, attachment models.Attachment) (models.Attachment, error) {
attachment.Uuid = uuid.NewString() attachment.Uuid = uuid.NewString()
attachment.Size = file.Size attachment.Size = file.Size