如何使缓存保持最新 [英] how to keep caching up to date

查看:48
本文介绍了如何使缓存保持最新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当 memecached 或 Redis 用于数据存储缓存时.当值改变时缓存是如何更新的?

when memecached or Redis is used for data-storage caching. How is the cache being updated when the value changed?

例如.如果我第一次从缓存中读取 key1 并且它错过了,那么我拉取 value1 并将 key1=value1 放入缓存中.

For, example. If I read key1 from cache the first time and it missed, then I pull value1 and put key1=value1 into cache.

之后如果key1的值变成了value2.缓存中的值如何更新或失效?

After that if the value of key1 changed to value2. How is value in cache updated or invalidated?

这是否意味着只要 key1 的值发生变化.应用程序或数据库需要检查这个key1是否在缓存中并更新它?

Does that mean whenever there is a change on key1's value. Either the application or database need to check if this key1 is in cache and update it?

推荐答案

既然使用了缓存,就不得不容忍数据不一致的问题,即在某个时间点,缓存中的数据是与数据库中的数据不同.

Since you are using a cache, you have to tolerate the data inconsistency problem, i.e. at some time point, data in cache is different from data in database.

无论何时更改值,您都不需要更新缓存中的值.否则,整个缓存系统将非常复杂(例如,您必须维护已缓存的键列表),并且可能没有必要这样做(例如,键值可能只使用一次,不需要再更新一下).

You don't need to update the value in cache, whenever the value has been changed. Otherwise, the whole cache system will be very complicated (e.g. you have to maintain a list of keys that have been cached), and it also might be unnecessary to do that (e.g. the key-value might be used only once, and no need to update it any more).

我们如何更新缓存中的数据并保持缓存系统简单?

How can we update the data in cache and keep the cache system simple?

通常,除了设置或更新缓存中的键值对之外,我们还会为每个键设置TIMEOUT.之后,客户端可以从缓存中获取键值对.但是,如果键达到超时,缓存系统会从缓存中删除键值对.这称为密钥已过期.下一次,客户端尝试从缓存中获取该密钥,将一无所获.这称为CACHE MISS.在这种情况下,客户端必须从数据库中获取键值对,并使用新的超时时间将其更新到缓存.

Normally, besides setting or updating a key-value pair in cache, we also set a TIMEOUT for each key. After that, client can get the key-value pair from the cache. However, if a key reaches the timeout, the cache system removes the key-value pair from the cache. This is called THE KEY HAS BEEN EXPIRED. The next time, the client trying to get that key from cache, will get nothing. This is called CACHE MISS. In this case, client has to get the key-value pair from database, and update it to cache with a new timeout.

如果数据库中的数据已经更新,而缓存中的key没有过期,客户端会得到不一致的数据.但是,当密钥已过期时,它的值将从数据库中检索并由某些客户端插入缓存中.之后,其他客户端将获得更新的数据,直到数据再次更改.

If the data has been updated in database, while the key has NOT been expired in cache, client will get inconsistent data. However, when the key has been expired, its value will be retrieved from database and inserted into cache by some client. After that, other clients will get updated data until the data has been changed again.

如何设置超时时间?

通常,有两种过期策略:

Normally, there're two kinds of expiration policy:

  1. N 秒/分钟/小时后过期...
  2. 在未来某个时间点到期,例如2017/7/30 00:00:00 到期
  1. Expire in N seconds/minutes/hours...
  2. Expire at some future timepoint, e.g. expire at 2017/7/30 00:00:00

大的超时时间可以大大减少数据库的负载,而数据可能会长时间过期.一个小的超时时间可以使数据尽可能保持最新,而数据库的负载会很重.所以你必须在设计超时时权衡取舍.

A large timeout can largely reduce the load of database, while the data might be out-of-date for a long time. A small timeout can keep the data up-to-date as much as possible, while the database will have a heavy load. So you have to balance the trade-off when designing the timeout.

Redis 如何使密钥过期?

How does Redis expire keys?

Redis 有两种方法可以使密钥过期:

Redis has two ways to expire keys:

  1. 当客户端尝试对某个键进行操作时,Redis 会检查该键是否已达到超时.如果是,Redis 将删除该键,并表现为该键不存在.这样,Redis 就可以确保客户端不会得到过期的数据.
  2. Redis 还有一个过期线程,它以配置的频率对密钥进行采样.如果键达到超时,Redis 会删除这些键.通过这种方式,Redis 可以加速密钥过期过程.

这篇关于如何使缓存保持最新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆