Skip to content

Conversation

@LanceAdd
Copy link
Member

@LanceAdd LanceAdd commented Jul 16, 2025

  1. 进行数据库查询Count()时使用强制缓存Force = true如果count(1) = 0, 那么缓存内容会是
{
    "Result": null,
    "FirstResultColumn": "COUNT(1)"
}

导致第二次查询时走缓存但是拿到null然后再查一次数据库,强制缓存等于失效,流量较大时容易缓存击穿
debug原因是缓存查询结果时,对count的结果判断isEmpty(),但是IsEmpty()对值为0的整数类型判断为true,然后将result=nil缓存起来,所以当Force = true时COUNT(1)=0应该存入缓存

{
    "Result": [
        {
            "COUNT(1)": 0
        }
    ],
    "FirstResultColumn": "COUNT(1)"
}
SCR-20250716-jadl
  1. 当前All()查询如果查不到数据返回的是nil,更推荐返回空切片

  2. 不启用强制缓存时,result == nil的结果缓存后还是会导致下次查询走数据库,缓存null没有意义,直接return即可

@houseme houseme requested review from Copilot, gqcn and hailaz and removed request for Copilot September 1, 2025 06:36
Copy link
Contributor

Copilot AI left a 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 database caching issues where forced cache policies were being invalidated for COUNT queries and improves empty result handling. The main problem was that COUNT(1) = 0 results were being treated as empty and cached as null, causing subsequent queries to bypass the cache.

  • Fixed forced cache invalidation for COUNT queries returning 0
  • Changed All() queries to return empty slices instead of nil for no results
  • Prevented caching of null results when forced caching is disabled

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

File Description
database/gdb/gdb_model_select.go Initialize empty Result{} instead of nil for no results
database/gdb/gdb_model_cache.go Separate cache logic for COUNT vs other queries and skip null caching when force is disabled
contrib/drivers/*/*_z_unit_model_test.go Update test assertions to expect empty slices instead of nil

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- 在 selectCacheItem 结构体中新增 IsCached 字段用于区分缓存未命中与已缓存空结果
- 修改缓存读取逻辑,确保即使是空结果也能被正确识别为有效缓存项
- 更新缓存写入逻辑,在缓存空结果时也标记 IsCached 为 true
- 移除旧的针对 Value/Count 操作的特殊处理逻辑
- 统一处理缓存穿透问题:当查询结果为空且 Force=false 时不缓存,Force=true 时缓存空结果
- 确保缓存空结果时使用空数组而非 nil,提升缓存行为一致性
- 调整空结果判断条件,增强对Count和Value操作的特殊处理
- 当Force=true时确保空结果也能被正确缓存
- 新增针对SelectTypeValue、SelectTypeArray和SelectTypeCount类型的空值检测
- 改进内部列数据获取及空值验证逻辑
- 避免因查询无数据导致的缓存穿透问题
@LanceAdd LanceAdd requested a review from gqcn December 17, 2025 13:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants