如何保持最新状态 [英] how to keep caching up to date

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

问题描述

当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 .之后,客户端可以从缓存中获取键值对.但是,如果键达到超时,则高速缓存系统将从高速缓存中删除键/值对.这称为THE KEY HAS BEEN EXPIRED.下次,客户端尝试从缓存中获取该密钥时,将一无所获.这称为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.

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

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.

如何设置超时时间?

How to set the timeout?

通常,有两种到期政策:

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天全站免登陆