⚡ Add cache into metadata fetching
This commit is contained in:
parent
55491ea3c9
commit
e3eaa5ba20
48
.idea/workspace.xml
generated
48
.idea/workspace.xml
generated
@ -4,9 +4,11 @@
|
||||
<option name="autoReloadType" value="ALL" />
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="18dd0d68-b4b8-40db-9734-9119b5c848bd" name="更改" comment=":arrow_up: Upgrade Passport to fix bug">
|
||||
<list default="true" id="18dd0d68-b4b8-40db-9734-9119b5c848bd" name="更改" comment=":ambulance: Fix getting user panic">
|
||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/pkg/internal/server/api/attachments_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/api/attachments_api.go" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/pkg/internal/services/attachments.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/attachments.go" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/settings.toml" beforeDir="false" afterPath="$PROJECT_DIR$/settings.toml" afterDir="false" />
|
||||
</list>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
@ -36,27 +38,28 @@
|
||||
<option name="hideEmptyMiddlePackages" value="true" />
|
||||
<option name="showLibraryContents" value="true" />
|
||||
</component>
|
||||
<component name="PropertiesComponent">{
|
||||
"keyToString": {
|
||||
"DefaultGoTemplateProperty": "Go File",
|
||||
"Go Build.Backend.executor": "Run",
|
||||
"Go 构建.Backend.executor": "Run",
|
||||
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||
"RunOnceActivity.go.formatter.settings.were.checked": "true",
|
||||
"RunOnceActivity.go.migrated.go.modules.settings": "true",
|
||||
"RunOnceActivity.go.modules.automatic.dependencies.download": "true",
|
||||
"RunOnceActivity.go.modules.go.list.on.any.changes.was.set": "true",
|
||||
"git-widget-placeholder": "master",
|
||||
"go.import.settings.migrated": "true",
|
||||
"go.sdk.automatically.set": "true",
|
||||
"last_opened_file_path": "/Users/littlesheep/Documents/Projects/Hydrogen/Paperclip/pkg/internal/grpc",
|
||||
"node.js.detected.package.eslint": "true",
|
||||
"node.js.selected.package.eslint": "(autodetect)",
|
||||
"nodejs_package_manager_path": "npm",
|
||||
"run.code.analysis.last.selected.profile": "pProject Default",
|
||||
"settings.editor.selected.configurable": "preferences.lookFeel"
|
||||
<component name="PropertiesComponent"><![CDATA[{
|
||||
"keyToString": {
|
||||
"DefaultGoTemplateProperty": "Go File",
|
||||
"Go Build.Backend.executor": "Run",
|
||||
"Go 构建.Backend.executor": "Run",
|
||||
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||
"RunOnceActivity.go.formatter.settings.were.checked": "true",
|
||||
"RunOnceActivity.go.migrated.go.modules.settings": "true",
|
||||
"RunOnceActivity.go.modules.automatic.dependencies.download": "true",
|
||||
"RunOnceActivity.go.modules.go.list.on.any.changes.was.set": "true",
|
||||
"git-widget-placeholder": "master",
|
||||
"go.import.settings.migrated": "true",
|
||||
"go.sdk.automatically.set": "true",
|
||||
"last_opened_file_path": "/Users/littlesheep/Documents/Projects/Hydrogen/Paperclip/pkg/internal/grpc",
|
||||
"node.js.detected.package.eslint": "true",
|
||||
"node.js.selected.package.eslint": "(autodetect)",
|
||||
"nodejs_package_manager_path": "npm",
|
||||
"run.code.analysis.last.selected.profile": "pProject Default",
|
||||
"settings.editor.selected.configurable": "preferences.lookFeel",
|
||||
"vue.rearranger.settings.migration": "true"
|
||||
}
|
||||
}</component>
|
||||
}]]></component>
|
||||
<component name="RecentsManager">
|
||||
<key name="CopyFile.RECENT_KEYS">
|
||||
<recent name="$PROJECT_DIR$/pkg/internal/grpc" />
|
||||
@ -113,7 +116,8 @@
|
||||
<MESSAGE value=" :sparkles: Add health check" />
|
||||
<MESSAGE value=":arrow_up: Upgrade Passport and use Hyper SDK" />
|
||||
<MESSAGE value=":arrow_up: Upgrade Passport to fix bug" />
|
||||
<option name="LAST_COMMIT_MESSAGE" value=":arrow_up: Upgrade Passport to fix bug" />
|
||||
<MESSAGE value=":ambulance: Fix getting user panic" />
|
||||
<option name="LAST_COMMIT_MESSAGE" value=":ambulance: Fix getting user panic" />
|
||||
</component>
|
||||
<component name="VgoProject">
|
||||
<settings-migrated>true</settings-migrated>
|
||||
|
@ -159,11 +159,11 @@ func updateAttachmentMeta(c *fiber.Ctx) error {
|
||||
attachment.Metadata = data.Metadata
|
||||
attachment.IsMature = data.IsMature
|
||||
|
||||
if err := database.C.Save(&attachment).Error; err != nil {
|
||||
if attachment, err := services.UpdateAttachment(attachment); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else {
|
||||
return c.JSON(attachment)
|
||||
}
|
||||
|
||||
return c.JSON(attachment)
|
||||
}
|
||||
|
||||
func deleteAttachment(c *fiber.Ctx) error {
|
||||
|
@ -13,23 +13,27 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const metadataCacheLimit = 512
|
||||
|
||||
var metadataCache = make(map[uint]models.Attachment)
|
||||
|
||||
func GetAttachmentByID(id uint) (models.Attachment, error) {
|
||||
if val, ok := metadataCache[id]; ok {
|
||||
return val, nil
|
||||
}
|
||||
|
||||
var attachment models.Attachment
|
||||
if err := database.C.Where(models.Attachment{
|
||||
BaseModel: models.BaseModel{ID: id},
|
||||
}).First(&attachment).Error; err != nil {
|
||||
return attachment, err
|
||||
} else {
|
||||
if len(metadataCache) > metadataCacheLimit {
|
||||
clear(metadataCache)
|
||||
}
|
||||
metadataCache[id] = attachment
|
||||
}
|
||||
return attachment, nil
|
||||
}
|
||||
|
||||
func GetAttachmentByUUID(id string) (models.Attachment, error) {
|
||||
var attachment models.Attachment
|
||||
if err := database.C.Where(models.Attachment{
|
||||
Uuid: id,
|
||||
}).First(&attachment).Error; err != nil {
|
||||
return attachment, err
|
||||
}
|
||||
return attachment, nil
|
||||
}
|
||||
|
||||
@ -61,7 +65,7 @@ func NewAttachmentMetadata(tx *gorm.DB, user models.Account, file *multipart.Fil
|
||||
attachment.Name = file.Filename
|
||||
attachment.AccountID = user.ID
|
||||
|
||||
// If user didn't provide file mimetype manually, we gotta to detect it
|
||||
// If the user didn't provide file mimetype manually, we have to detect it
|
||||
if len(attachment.MimeType) == 0 {
|
||||
if ext := filepath.Ext(attachment.Name); len(ext) > 0 {
|
||||
// Detect mimetype by file extensions
|
||||
@ -87,11 +91,29 @@ func NewAttachmentMetadata(tx *gorm.DB, user models.Account, file *multipart.Fil
|
||||
|
||||
if err := tx.Save(&attachment).Error; err != nil {
|
||||
return attachment, linked, fmt.Errorf("failed to save attachment record: %v", err)
|
||||
} else {
|
||||
if len(metadataCache) > metadataCacheLimit {
|
||||
clear(metadataCache)
|
||||
}
|
||||
metadataCache[attachment.ID] = attachment
|
||||
}
|
||||
|
||||
return attachment, linked, nil
|
||||
}
|
||||
|
||||
func UpdateAttachment(item models.Attachment) (models.Attachment, error) {
|
||||
if err := database.C.Save(&item).Error; err != nil {
|
||||
return item, err
|
||||
} else {
|
||||
if len(metadataCache) > metadataCacheLimit {
|
||||
clear(metadataCache)
|
||||
}
|
||||
metadataCache[item.ID] = item
|
||||
}
|
||||
|
||||
return item, nil
|
||||
}
|
||||
|
||||
func DeleteAttachment(item models.Attachment) error {
|
||||
var dupeCount int64
|
||||
if err := database.C.
|
||||
@ -103,6 +125,8 @@ func DeleteAttachment(item models.Attachment) error {
|
||||
|
||||
if err := database.C.Delete(&item).Error; err != nil {
|
||||
return err
|
||||
} else {
|
||||
delete(metadataCache, item.ID)
|
||||
}
|
||||
|
||||
if dupeCount != -1 && dupeCount <= 1 {
|
||||
|
@ -27,7 +27,7 @@ prefix = "paperclip_"
|
||||
|
||||
[destinations.local]
|
||||
type = "local"
|
||||
path = "/uploads"
|
||||
path = "uploads"
|
||||
|
||||
[destinations.s3]
|
||||
type = "s3"
|
||||
|
Loading…
Reference in New Issue
Block a user