-
Notifications
You must be signed in to change notification settings - Fork 0
cache
WinChua edited this page Apr 28, 2020
·
1 revision
场景: 读多写少, 数据实时性要求不高,数据改变频次不高
read: 1. miss: 读取db, set 缓存; 2. hit: return
write: 1. write db; 2. 手动让缓存失效
主要坑点在写缓存的流程中:
- 更新DB--> 更新缓存: 两个并发写操作可能会导致 脏数据,
thread 1: update db ---------------------------> update cache
==============================================================
thread 2: update db ----------> update cahce
对于thread 2来说, 在两个thread完成之后, 读到的是thread 1 的cache, cache与db的数据不一致;
- 删除缓存, 更新db: 并发读写导致 脏数据 ,
thread 1: delete cache ----------------------------------------> update db
==========================================================================
thread 2: cache miss ---> read db ---> update cache
在缓存失效前, 会有一段时间的cache与db数据不一致
- 更新db, 删除缓存: (比较推荐)
- read: 调用服务从缓存中读取数据, 如果缓存服务命中, 则返回数据; 否则 缓存服务自动从db中读取数据, 设置缓存, 返回数据, 无需调用方手动设置缓存;
- write: 如果缓存没有命中, 更新db, 直接返回更新后的数据; 如果缓存命中了, 缓存服务更新缓存, 同步更新db, 将结果返回给主调服务;