⚡ Optimize list attachment api
This commit is contained in:
parent
13893e87f7
commit
3e000b6f9e
@ -20,6 +20,8 @@ func listAttachment(c *fiber.Ctx) error {
|
||||
|
||||
tx := database.C
|
||||
|
||||
needQuery := true
|
||||
|
||||
var result = make([]models.Attachment, take)
|
||||
var idxList []uint
|
||||
|
||||
@ -40,6 +42,7 @@ func listAttachment(c *fiber.Ctx) error {
|
||||
}
|
||||
}
|
||||
tx = tx.Where("id IN ?", pendingQueryId)
|
||||
needQuery = len(pendingQueryId) > 0
|
||||
} else {
|
||||
// Do sort this when doesn't filter by the id
|
||||
// Because the sort will mess up the result
|
||||
@ -64,6 +67,8 @@ func listAttachment(c *fiber.Ctx) error {
|
||||
if err := countTx.Model(&models.Attachment{}).Count(&count).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
if needQuery {
|
||||
var out []models.Attachment
|
||||
if err := tx.Offset(offset).Limit(take).Preload("Account").Find(&out).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
@ -80,6 +85,11 @@ func listAttachment(c *fiber.Ctx) error {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, item := range result {
|
||||
services.CacheAttachment(item.ID, item)
|
||||
}
|
||||
|
||||
return c.JSON(fiber.Map{
|
||||
"count": count,
|
||||
|
@ -55,6 +55,10 @@ func GetAttachmentCache(id uint) (models.Attachment, bool) {
|
||||
return models.Attachment{}, false
|
||||
}
|
||||
|
||||
func CacheAttachment(id uint, item models.Attachment) {
|
||||
metadataCache.Store(id, item)
|
||||
}
|
||||
|
||||
func NewAttachmentMetadata(tx *gorm.DB, user models.Account, file *multipart.FileHeader, attachment models.Attachment) (models.Attachment, error) {
|
||||
attachment.Uuid = uuid.NewString()
|
||||
attachment.Size = file.Size
|
||||
|
Loading…
Reference in New Issue
Block a user