Sign in
Sign up
Explore
Enterprise
Education
Search
Help
Terms of use
About Us
Explore
Enterprise
Education
Gitee Premium
Gitee AI
AI teammates
Sign in
Sign up
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Cancel
Sign in
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
Watch
Unwatch
Watching
Releases Only
Ignoring
3
Star
47
Fork
23
DreamCoders
/
CoderGuide
Code
Issues
1169
Pull Requests
0
Wiki
Insights
Pipelines
Service
JavaDoc
PHPDoc
Quality Analysis
Jenkins for Gitee
Tencent CloudBase
Tencent Cloud Serverless
悬镜安全
Aliyun SAE
Codeblitz
SBOM
DevLens
Don’t show this again
Update failed. Please try again later!
Remove this flag
Content Risk Flag
This task is identified by
as the content contains sensitive information such as code security bugs, privacy leaks, etc., so it is only accessible to contributors of this repository.
缓存穿透问题
Backlog
#IAJL40
陌生人
owner
Opened this issue
2024-08-13 10:12
<blockquote><strong>缓存穿透-数据库没有对应的数据空查询</strong></blockquote><p><span style="color: rgb(25, 27, 31); background-color: rgb(255, 255, 255);">先来看一个常见的缓存使用方式:读请求来了,先查下缓存,缓存有值命中,就直接返回;缓存没命中,就去查数据库,然后把数据库的值更新到缓存,再返回。</span></p><p><img src="https://jsd.onmicrosoft.cn/gh/iGaoWei/codercdn@master/question/20240627/2024062710531929714.png" alt="https://jsd.onmicrosoft.cn/gh/iGaoWei/codercdn@master/question/20240627/2024062710531929714.png" data-href="" style=""/></p><p style="text-align: start;">读取缓存</p><p style="text-align: start;"><strong>缓存穿透</strong>:指查询一个一定不存在的数据,由于缓存是不命中时需要从数据库查询,查不到数据则不写入缓存,这将导致这个不存在的数据每次请求都要到数据库去查询,进而给数据库带来压力。</p><blockquote style="text-align: start;">通俗点说,读请求访问时,缓存和数据库都没有某个值,这样就会导致每次对这个值的查询请求都会穿透到数据库,这就是缓存穿透。</blockquote><h4 style="text-align: start;">缓存穿透一般都是这几种情况产生的:</h4><p style="text-align: start;"><strong>业务不合理的设计</strong>,比如大多数用户都没开守护,但是你的每个请求都去缓存,查询某个userid查询有没有守护。</p><p style="text-align: start;"><strong>业务/运维/开发失误的操作</strong>,比如缓存和数据库的数据都被误删除了。</p><p style="text-align: start;"><strong>黑客非法请求攻击</strong>,比如黑客故意捏造大量非法请求,以读取不存在的业务数据。</p><h4 style="text-align: start;"><strong>如何避免缓存穿透呢?</strong> 一般有三种方法。</h4><p style="text-align: start;">1.如果是非法请求,我们在API入口,对参数进行校验,过滤非法值。</p><p style="text-align: start;">2.如果查询数据库为空,我们可以给缓存设置个空值,或者默认值。但是如有有写请求进来的话,需要更新缓存哈,以保证缓存一致性,同时,最后给缓存设置适当的过期时间。(业务上比较常用,简单有效)</p><p style="text-align: start;">3.使用布隆过滤器快速判断数据是否存在。即一个查询请求过来时,先通过布隆过滤器判断值是否存在,存在才继续往下查。</p><blockquote style="text-align: start;">布隆过滤器原理:它由初始值为0的位图数组和N个哈希函数组成。一个对一个key进行N个hash算法获取N个值,在比特数组中将这N个值散列后设定为1,然后查的时候如果特定的这几个位置都为1,那么布隆过滤器判断该key存在。</blockquote>
<blockquote><strong>缓存穿透-数据库没有对应的数据空查询</strong></blockquote><p><span style="color: rgb(25, 27, 31); background-color: rgb(255, 255, 255);">先来看一个常见的缓存使用方式:读请求来了,先查下缓存,缓存有值命中,就直接返回;缓存没命中,就去查数据库,然后把数据库的值更新到缓存,再返回。</span></p><p><img src="https://jsd.onmicrosoft.cn/gh/iGaoWei/codercdn@master/question/20240627/2024062710531929714.png" alt="https://jsd.onmicrosoft.cn/gh/iGaoWei/codercdn@master/question/20240627/2024062710531929714.png" data-href="" style=""/></p><p style="text-align: start;">读取缓存</p><p style="text-align: start;"><strong>缓存穿透</strong>:指查询一个一定不存在的数据,由于缓存是不命中时需要从数据库查询,查不到数据则不写入缓存,这将导致这个不存在的数据每次请求都要到数据库去查询,进而给数据库带来压力。</p><blockquote style="text-align: start;">通俗点说,读请求访问时,缓存和数据库都没有某个值,这样就会导致每次对这个值的查询请求都会穿透到数据库,这就是缓存穿透。</blockquote><h4 style="text-align: start;">缓存穿透一般都是这几种情况产生的:</h4><p style="text-align: start;"><strong>业务不合理的设计</strong>,比如大多数用户都没开守护,但是你的每个请求都去缓存,查询某个userid查询有没有守护。</p><p style="text-align: start;"><strong>业务/运维/开发失误的操作</strong>,比如缓存和数据库的数据都被误删除了。</p><p style="text-align: start;"><strong>黑客非法请求攻击</strong>,比如黑客故意捏造大量非法请求,以读取不存在的业务数据。</p><h4 style="text-align: start;"><strong>如何避免缓存穿透呢?</strong> 一般有三种方法。</h4><p style="text-align: start;">1.如果是非法请求,我们在API入口,对参数进行校验,过滤非法值。</p><p style="text-align: start;">2.如果查询数据库为空,我们可以给缓存设置个空值,或者默认值。但是如有有写请求进来的话,需要更新缓存哈,以保证缓存一致性,同时,最后给缓存设置适当的过期时间。(业务上比较常用,简单有效)</p><p style="text-align: start;">3.使用布隆过滤器快速判断数据是否存在。即一个查询请求过来时,先通过布隆过滤器判断值是否存在,存在才继续往下查。</p><blockquote style="text-align: start;">布隆过滤器原理:它由初始值为0的位图数组和N个哈希函数组成。一个对一个key进行N个hash算法获取N个值,在比特数组中将这N个值散列后设定为1,然后查的时候如果特定的这几个位置都为1,那么布隆过滤器判断该key存在。</blockquote>
Comments (
0
)
Sign in
to comment
Status
Backlog
Backlog
Doing
Done
Closed
Assignees
Not set
Labels
Redis
Not set
Label settings
Milestones
No related milestones
No related milestones
Pull Requests
None yet
None yet
Successfully merging a pull request will close this issue.
Branches
No related branch
Branches (
-
)
Tags (
-
)
Planed to start   -   Planed to end
-
Top level
Not Top
Top Level: High
Top Level: Medium
Top Level: Low
Priority
Not specified
Serious
Main
Secondary
Unimportant
参与者(1)
1
https://gitee.com/DreamCoders/CoderGuide.git
git@gitee.com:DreamCoders/CoderGuide.git
DreamCoders
CoderGuide
CoderGuide
Going to Help Center
Search
Git 命令在线学习
如何在 Gitee 导入 GitHub 仓库
Git 仓库基础操作
企业版和社区版功能对比
SSH 公钥设置
如何处理代码冲突
仓库体积过大,如何减小?
如何找回被删除的仓库数据
Gitee 产品配额说明
GitHub仓库快速导入Gitee及同步更新
什么是 Release(发行版)
将 PHP 项目自动发布到 packagist.org
Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register