Fix the RandString method cause the lag

This commit is contained in:
2024-08-18 17:16:14 +08:00
parent f32803acf4
commit e7f6637398
4 changed files with 40 additions and 57 deletions

View File

@ -60,8 +60,8 @@ func CacheAttachment(id uint, item models.Attachment) {
}
func NewAttachmentMetadata(tx *gorm.DB, user models.Account, file *multipart.FileHeader, attachment models.Attachment) (models.Attachment, error) {
attachment.Rid = RandString(16)
attachment.Uuid = uuid.NewString()
attachment.Rid = RandString(16)
attachment.Size = file.Size
attachment.Name = file.Filename
attachment.AccountID = user.ID

View File

@ -2,30 +2,14 @@ package services
import (
"math/rand"
"strings"
)
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const (
letterIdxBits = 6
letterIdxMask = 1<<letterIdxBits - 1
letterIdxMax = 63 / letterIdxBits
)
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func RandString(length int) string {
builder := strings.Builder{}
builder.Grow(length)
for idx, cache, remain := length-1, rand.Int63(), letterIdxMax; idx >= 0; {
if remain == 0 {
cache, remain = rand.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
builder.WriteByte(letterBytes[idx])
idx--
}
cache >>= letterIdxBits
remain--
builder := make([]rune, length)
for i := range builder {
builder[i] = letters[rand.Intn(len(letters))]
}
return builder.String()
return string(builder)
}

View File

@ -51,7 +51,8 @@ func RunMarkDeletionTask() {
for _, pool := range pendingPools {
lifecycle := fmt.Sprintf("%d seconds", *pool.Config.Data().ExistLifecycle)
tx := database.C.
Where("pool_id = ? AND created_at < NOW() - INTERVAL ?", pool.ID, lifecycle).
Where("pool_id = ?", pool.ID).
Where("created_at < NOW() - INTERVAL ?", lifecycle).
Updates(&models.Attachment{CleanedAt: lo.ToPtr(time.Now())})
log.Info().
Str("pool", pool.Alias).