rdasxy
rdasxy

Reputation: 1661

How does memcached behave when data has changed?

Reading this brief example of using memcached with PHP, I was wondering how memcached knows when a request for data needs to actually come from the database instead of coming from the cache.

Upvotes: 1

Views: 957

Answers (3)

deceze
deceze

Reputation: 522101

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.

Upvotes: 3

Martin Thurau
Martin Thurau

Reputation: 7644

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

Upvotes: 3

zad
zad

Reputation: 3415

I think you need to program that logic.

e.g. When you update the database then update the memcached value associated with that key, or make that key expire.

Upvotes: 1

Related Questions