Reputation: 1661
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
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
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
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