Summary
-------
This commit unifies soft-delete behaviour across meta tables and
introduces an in-memory cache for model pricing look-ups to improve
throughput under high concurrency.
Details
-------
Soft-delete consistency
• PrefillGroup / Vendor / Model
– Added `gorm.DeletedAt` field with `json:"-" gorm:"index"`.
– Replaced plain `uniqueIndex` with partial unique indexes
`uniqueIndex:<name>,where:deleted_at IS NULL`
allowing duplicate keys after logical deletion while preserving
uniqueness for active rows.
• Imports updated to include `gorm.io/gorm`.
• JSON output now hides `deleted_at`, matching existing tables.
High-throughput pricing cache
• model/pricing.go
– Added thread-safe maps `modelEnableGroups` & `modelQuotaTypeMap`
plus RW-mutex for O(1) access.
– `updatePricing()` now refreshes these maps alongside `pricingMap`.
• model/model_extra.go
– Rewrote `GetModelEnableGroups` & `GetModelQuotaType` to read from
the new maps, falling back to automatic refresh via `GetPricing()`.
Misc
• Retained `RefreshPricing()` helper for immediate cache invalidation
after admin actions.
• All modified files pass linter; no breaking DB migrations required
(handled by AutoMigrate).
Result
------
– Soft-delete logic is transparent, safe, and allows record “revival”.
– Pricing-related queries are now constant-time, reducing CPU usage and
latency under load.
78 lines
2.3 KiB
Go
78 lines
2.3 KiB
Go
package model
|
||
|
||
import (
|
||
"one-api/common"
|
||
|
||
"gorm.io/gorm"
|
||
)
|
||
|
||
// Vendor 用于存储供应商信息,供模型引用
|
||
// Name 唯一,用于在模型中关联
|
||
// Icon 采用 @lobehub/icons 的图标名,前端可直接渲染
|
||
// Status 预留字段,1 表示启用
|
||
// 本表同样遵循 3NF 设计范式
|
||
|
||
type Vendor struct {
|
||
Id int `json:"id"`
|
||
Name string `json:"name" gorm:"size:128;not null;uniqueIndex:uk_vendor_name,where:deleted_at IS NULL"`
|
||
Description string `json:"description,omitempty" gorm:"type:text"`
|
||
Icon string `json:"icon,omitempty" gorm:"type:varchar(128)"`
|
||
Status int `json:"status" gorm:"default:1"`
|
||
CreatedTime int64 `json:"created_time" gorm:"bigint"`
|
||
UpdatedTime int64 `json:"updated_time" gorm:"bigint"`
|
||
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
|
||
}
|
||
|
||
// Insert 创建新的供应商记录
|
||
func (v *Vendor) Insert() error {
|
||
now := common.GetTimestamp()
|
||
v.CreatedTime = now
|
||
v.UpdatedTime = now
|
||
return DB.Create(v).Error
|
||
}
|
||
|
||
// Update 更新供应商记录
|
||
func (v *Vendor) Update() error {
|
||
v.UpdatedTime = common.GetTimestamp()
|
||
return DB.Save(v).Error
|
||
}
|
||
|
||
// Delete 软删除供应商
|
||
func (v *Vendor) Delete() error {
|
||
return DB.Delete(v).Error
|
||
}
|
||
|
||
// GetVendorByID 根据 ID 获取供应商
|
||
func GetVendorByID(id int) (*Vendor, error) {
|
||
var v Vendor
|
||
err := DB.First(&v, id).Error
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return &v, nil
|
||
}
|
||
|
||
// GetAllVendors 获取全部供应商(分页)
|
||
func GetAllVendors(offset int, limit int) ([]*Vendor, error) {
|
||
var vendors []*Vendor
|
||
err := DB.Offset(offset).Limit(limit).Find(&vendors).Error
|
||
return vendors, err
|
||
}
|
||
|
||
// SearchVendors 按关键字搜索供应商
|
||
func SearchVendors(keyword string, offset int, limit int) ([]*Vendor, int64, error) {
|
||
db := DB.Model(&Vendor{})
|
||
if keyword != "" {
|
||
like := "%" + keyword + "%"
|
||
db = db.Where("name LIKE ? OR description LIKE ?", like, like)
|
||
}
|
||
var total int64
|
||
if err := db.Count(&total).Error; err != nil {
|
||
return nil, 0, err
|
||
}
|
||
var vendors []*Vendor
|
||
if err := db.Offset(offset).Limit(limit).Order("id DESC").Find(&vendors).Error; err != nil {
|
||
return nil, 0, err
|
||
}
|
||
return vendors, total, nil
|
||
} |