gf icon indicating copy to clipboard operation
gf copied to clipboard

fix(database/gdb): Fixed the bug that the forced cache policy was invalidated

Open LanceAdd opened this issue 5 months ago • 0 comments

  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即可

LanceAdd avatar Jul 16 '25 02:07 LanceAdd