Caching patterns

来自WHY42
Riguz留言 | 贡献2024年7月23日 (二) 03:19的版本 →‎Cache-Aside (Lazy Loading)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

When you are caching data from your database, there are caching patterns for Redis and Memcached that you can implement, including proactive and reactive approaches. The patterns you choose to implement should be directly related to your caching and application objectives.

Two common approaches are cache-aside or lazy loading (a reactive approach) and write-through (a proactive approach). A cache-aside cache is updated after the data is requested. A write-through cache is updated immediately when the primary database is updated. With both approaches, the application is essentially managing what data is being cached and for how long. [1]

Cache-Aside (Lazy Loading)

  • 失效:应用程序先从cache取数据,没有得到,则从数据库中取数据,成功后,放到缓存中。
  • 命中:应用程序从cache中取数据,取到后返回。
  • 更新:先把数据存到数据库中,成功后,再让缓存失效。

[2]

为什么不是写完数据库后更新缓存? 主要是怕两个并发的写操作导致脏数据。

Read/Write Through

Write Behind Caching