MCPcopy
hub / github.com/fish2018/pansou / injectMainCacheToAsyncPlugins

Function injectMainCacheToAsyncPlugins

service/search_service.go:233–348  ·  view source on GitHub ↗

injectMainCacheToAsyncPlugins 将主缓存系统注入到异步插件中

(pluginManager *plugin.PluginManager, mainCache *cache.EnhancedTwoLevelCache)

Source from the content-addressed store, hash-verified

231
232// injectMainCacheToAsyncPlugins 将主缓存系统注入到异步插件中
233func injectMainCacheToAsyncPlugins(pluginManager *plugin.PluginManager, mainCache *cache.EnhancedTwoLevelCache) {
234 // 如果缓存或插件管理器不可用,直接返回
235 if mainCache == nil || pluginManager == nil {
236 return
237 }
238
239 // 设置全局序列化器,确保异步插件与主程序使用相同的序列化格式
240 serializer := mainCache.GetSerializer()
241 if serializer != nil {
242 plugin.SetGlobalCacheSerializer(serializer)
243 }
244
245 // 创建缓存更新函数(支持IsFinal参数)- 接收原始数据并与现有缓存合并
246 cacheUpdater := func(key string, newResults []model.SearchResult, ttl time.Duration, isFinal bool, keyword string, pluginName string) error {
247 // 优化:如果新结果为空,跳过缓存更新(避免无效操作)
248 if len(newResults) == 0 {
249 return nil
250 }
251
252 // 获取现有缓存数据进行合并
253 var finalResults []model.SearchResult
254 if existingData, hit, err := mainCache.Get(key); err == nil && hit {
255 var existingResults []model.SearchResult
256 if err := mainCache.GetSerializer().Deserialize(existingData, &existingResults); err == nil {
257 // 合并新旧结果,去重保留最完整的数据
258 finalResults = mergeSearchResults(existingResults, newResults)
259 if config.AppConfig != nil && config.AppConfig.AsyncLogEnabled {
260 if keyword != "" {
261 fmt.Printf("🔄 [%s:%s] 更新缓存| 原有: %d + 新增: %d = 合并后: %d\n",
262 pluginName, keyword, len(existingResults), len(newResults), len(finalResults))
263 }
264 }
265 } else {
266 // 反序列化失败,使用新结果
267 finalResults = newResults
268 if config.AppConfig != nil && config.AppConfig.AsyncLogEnabled {
269 displayKey := key[:8] + "..."
270 if keyword != "" {
271 fmt.Printf("[异步插件 %s] 缓存反序列化失败,使用新结果: %s(关键词:%s) | 结果数: %d\n", pluginName, displayKey, keyword, len(newResults))
272 } else {
273 fmt.Printf("[异步插件 %s] 缓存反序列化失败,使用新结果: %s | 结果数: %d\n", pluginName, key, len(newResults))
274 }
275 }
276 }
277 } else {
278 // 无现有缓存,直接使用新结果
279 finalResults = newResults
280 if config.AppConfig != nil && config.AppConfig.AsyncLogEnabled {
281 displayKey := key[:8] + "..."
282 if keyword != "" {
283 fmt.Printf("[异步插件 %s] 初始缓存创建: %s(关键词:%s) | 结果数: %d\n", pluginName, displayKey, keyword, len(newResults))
284 } else {
285 fmt.Printf("[异步插件 %s] 初始缓存创建: %s | 结果数: %d\n", pluginName, key, len(newResults))
286 }
287 }
288 }
289
290 // 序列化合并后的结果

Callers 1

NewSearchServiceFunction · 0.85

Calls 12

SetGlobalCacheSerializerFunction · 0.92
mergeSearchResultsFunction · 0.85
GetSerializerMethod · 0.80
SetMemoryOnlyMethod · 0.80
HandleCacheOperationMethod · 0.80
SetBothLevelsMethod · 0.80
GetPluginsMethod · 0.80
DeserializeMethod · 0.65
SerializeMethod · 0.65
NameMethod · 0.65
GetMethod · 0.45
SetMainCacheUpdaterMethod · 0.45

Tested by

no test coverage detected