在 Result Cache 中缓存知识库查询错误以解决缓存穿透 - #19
Open
zwk4zhendeC wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
摘要
Result Cache条目,使其既能保存成功查询结果,也能保存缓存的查询错误。Null行,同时保持Bypass查询的错误返回行为不变。Null行写入Local Cache,确保重复查询仍经过 Result Cache,并能打印缓存错误日志。详情
本次变更调整了
wp-knowledge的 Result Cache 行为,使CachePolicy::UseGlobal查询在 provider 报错时也能缓存错误信息。变更前,Result Cache 只保存成功的
QueryResponse。如果远端 PostgreSQL 或 MySQL 查询持续失败,相同请求仍会每次调用 provider。变更后,
UseGlobal查询失败时,错误信息会按照与成功结果相同的 key 写入 Result Cache。后续相同查询会命中缓存错误,打印错误信息,并返回Null,不再调用 provider。错误缓存的 TTL 和容量与成功结果共享,当provider reload 后错误缓存会在过期时间后自然失效。核心改动
Response(Arc<QueryResponse>)Error(Arc<String>)UseGlobal查询路径中缓存 provider 错误。Null响应。__wp_kdb_error = NullLocal Cache写入。行为变化
CachePolicy::UseGlobal:[kdb] query error: ...,将错误写入 Result Cache,并返回Null。[kdb] cached query error: ...,返回Null,且不调用 provider。CachePolicy::Bypass:Err。TTL、容量和 reload 语义:
generation变化,错误缓存自然失效。变更文件
src/runtime.rsUseGlobal查询保存 provider 错误。src/facade.rsLocal Cache。验证
已执行:
cargo fmt --all cargo test runtime::tests cargo check结果:
runtime::tests通过。cargo check通过。测试覆盖
新增测试:
该测试验证:
UseGlobal查询失败时返回Null并保存错误。UseGlobal查询从 Result Cache 返回Null,不会再次调用 provider。Bypass查询仍返回Err,并会继续调用 provider。关联Issue:#18