简体   繁体   English

数据更改后,memcached的行为如何?

[英]How does memcached behave when data has changed?

通过阅读这个在PHP上使用memcached的简短示例 ,我想知道memcached如何知道何时对数据的请求实际上需要来自数据库而不是来自缓存。

It doesn't, you code does this. 并非如此,您只需编写代码即可。 In most cases you will do something like this: 在大多数情况下,您将执行以下操作:

key = /* build cache key somehow */
data = memcache.get(key)
if data is null:
  data = /* read data from database */
  cached.set(key, data)

// now you can use the data

It doesn't. 没有。 It comes down to your caching strategy. 这取决于您的缓存策略。 That is so with all forms of cache, a tradeoff between getting the latest data and getting some data quickly. 在所有形式的缓存中都是如此,这是在获取最新数据和快速获取一些数据之间的权衡。 If you need to have the data up to date, invalidate (delete) the cache when updating the original. 如果需要更新数据,请在更新原始数据时使缓存无效(删除)。 If performance is more important, let the cache expire by itself, at which point it will be renewed. 如果性能更重要,请让缓存自行过期,届时将对其进行更新。 Or something somewhere in-between. 或介于两者之间的某物。 It depends on your restrictions and goals. 这取决于您的限制和目标。

I think you need to program that logic. 我认为您需要对该逻辑进行编程。

eg When you update the database then update the memcached value associated with that key, or make that key expire. 例如,当您更新数据库时,请更新与该键关联的memcached值,或使该键过期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM