-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(database/gdb): Resolve the cache error overwriting caused by the use of fixed cache keys in pagination queries. #4339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a cache key collision issue in the GoFrame database ORM where AllAndCount
and ScanAndCount
methods were incorrectly sharing the same cache key for both count and select operations, causing incorrect query results.
- Introduces a new cache prefix constant
cachePrefixCountCache
to differentiate count queries from select queries - Modifies
AllAndCount
andScanAndCount
methods to use prefixed cache keys for count operations - Ensures count queries use
SelectCache:CountCache:
prefix to avoid cache conflicts with select queries
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
database/gdb/gdb.go | Adds new cache prefix constant for count operations |
database/gdb/gdb_model_select.go | Updates AllAndCount and ScanAndCount to use prefixed cache keys for count queries |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
database/gdb/gdb_model_select.go
Outdated
if countModel.cacheEnabled && m.cacheOption.Name != "" { | ||
countModel.cacheOption.Name = fmt.Sprintf(`%s%s`, cachePrefixCountCache, m.cacheOption.Name) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cache key modification logic is duplicated between AllAndCount and ScanAndCount methods. Consider extracting this into a helper method to improve maintainability and reduce code duplication.
Copilot uses AI. Check for mistakes.
- 在 Model 结构体中新增 pageCacheOption 字段用于分页缓存配置 - 新增 PageCache 方法支持为分页查询设置独立的缓存选项 - 修改分页查询逻辑,分别对 count 和数据查询应用不同的缓存策略- 优化缓存键名生成逻辑以适应分页场景 - 保持事务中禁用缓存特性的原有行为
执行这段查询后
g.DumpJson(result)
的结果是[ { "COUNT(1)": 5 } ]
,但是正确结果应该是五条用户信息,查看源代码后发现先执行的count查询和后来select查询都是直接使用了VIP
这个缓存key,在redis中实际缓存key是SelectCache:VIP
,第二步查询select获得的是count查询的缓存,所以查询结果是错的。 因此应该为Model
增加一个pageCacheOption []CacheOption
参数,再增加一个方法允许用户分别设置缓存参数然后
AllAndCount
在查询时分别给两个查询设置对应的缓存参数ScanAndCount
同理