Cache in batch metadata api

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

View File

@ -21,7 +21,7 @@ const metadataCacheLimit = 512
var metadataCache sync.Map
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
}
@ -48,6 +48,13 @@ func GetAttachmentByHash(hash string) (models.Attachment, error) {
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) {
attachment.Uuid = uuid.NewString()
attachment.Size = file.Size