refreshCacheInBackground 在后台刷新缓存
(
keyword string,
cacheKey string,
searchFunc func(*http.Client, string, map[string]interface{}) ([]model.SearchResult, error),
oldCache cachedResponse,
originalCacheKey string,
ext map[string]interface{},
)
| 1002 | |
| 1003 | // refreshCacheInBackground 在后台刷新缓存 |
| 1004 | func (p *BaseAsyncPlugin) refreshCacheInBackground( |
| 1005 | keyword string, |
| 1006 | cacheKey string, |
| 1007 | searchFunc func(*http.Client, string, map[string]interface{}) ([]model.SearchResult, error), |
| 1008 | oldCache cachedResponse, |
| 1009 | originalCacheKey string, |
| 1010 | ext map[string]interface{}, |
| 1011 | ) { |
| 1012 | // 确保ext不为nil |
| 1013 | if ext == nil { |
| 1014 | ext = make(map[string]interface{}) |
| 1015 | } |
| 1016 | |
| 1017 | // 注意:这里的cacheKey已经是插件特定的了,因为是从AsyncSearch传入的 |
| 1018 | |
| 1019 | // 检查是否有足够的工作槽 |
| 1020 | if !acquireWorkerSlot() { |
| 1021 | return |
| 1022 | } |
| 1023 | defer releaseWorkerSlot() |
| 1024 | |
| 1025 | // 记录刷新开始时间 |
| 1026 | refreshStart := time.Now() |
| 1027 | |
| 1028 | // 执行搜索 |
| 1029 | results, err := searchFunc(p.backgroundClient, keyword, ext) |
| 1030 | if err != nil || len(results) == 0 { |
| 1031 | return |
| 1032 | } |
| 1033 | |
| 1034 | // 创建合并结果集 |
| 1035 | mergedResults := make([]model.SearchResult, 0, len(results) + len(oldCache.Results)) |
| 1036 | |
| 1037 | // 创建已有结果ID的映射 |
| 1038 | existingIDs := make(map[string]bool) |
| 1039 | for _, r := range results { |
| 1040 | existingIDs[r.UniqueID] = true |
| 1041 | mergedResults = append(mergedResults, r) |
| 1042 | } |
| 1043 | |
| 1044 | // 添加旧结果中不存在的项 |
| 1045 | for _, r := range oldCache.Results { |
| 1046 | if !existingIDs[r.UniqueID] { |
| 1047 | mergedResults = append(mergedResults, r) |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | // 更新缓存 |
| 1052 | apiResponseCache.Store(cacheKey, cachedResponse{ |
| 1053 | Results: mergedResults, |
| 1054 | Timestamp: time.Now(), |
| 1055 | Complete: true, |
| 1056 | LastAccess: oldCache.LastAccess, |
| 1057 | AccessCount: oldCache.AccessCount, |
| 1058 | }) |
| 1059 | |
| 1060 | // 🔥 异步插件后台刷新完成时更新主缓存(标记为最终结果) |
| 1061 | p.updateMainCacheWithFinal(originalCacheKey, mergedResults, true) |
no test coverage detected