feat: enhance soft-delete handling & boost pricing cache performance

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.
This commit is contained in:
t0ng7u
2025-08-05 22:26:19 +08:00
parent f3a1f98add
commit d951485431
5 changed files with 69 additions and 41 deletions

View File

@@ -1,24 +1,34 @@
package model
// GetModelEnableGroups 返回指定模型名称可用的用户分组列表。
// 复用缓存的定价映射,避免额外的数据库查询
// 使用在 updatePricing() 中维护的缓存映射O(1) 读取,适合高并发场景
func GetModelEnableGroups(modelName string) []string {
for _, p := range GetPricing() {
if p.ModelName == modelName {
return p.EnableGroup
}
// 确保缓存最新
GetPricing()
if modelName == "" {
return make([]string, 0)
}
return make([]string, 0)
modelEnableGroupsLock.RLock()
groups, ok := modelEnableGroups[modelName]
modelEnableGroupsLock.RUnlock()
if !ok {
return make([]string, 0)
}
return groups
}
// GetModelQuotaType 返回指定模型的计费类型quota_type
// 复用缓存的定价映射,避免额外数据库查询
// 如果未找到对应模型,默认返回 0。
// 同样使用缓存映射,避免每次遍历定价切片
func GetModelQuotaType(modelName string) int {
for _, p := range GetPricing() {
if p.ModelName == modelName {
return p.QuotaType
}
GetPricing()
modelEnableGroupsLock.RLock()
quota, ok := modelQuotaTypeMap[modelName]
modelEnableGroupsLock.RUnlock()
if !ok {
return 0
}
return 0
return quota
}