How does the caching DbDependency work exactly?

I’m starting to use the built in query caching such as:


$sql = $db->createCommand('SELECT * FROM my_table WHERE something=1')->cache(3600)->queryAll();

Now I tried to find out how to delete the cache for this item once the database info has been changed, as I understand you can’t do it with the normal delete method as you need to provide a key, which you obviously don’t use with query caching.

Then I thought about using the DbDependency, however unsure how this is beneficial.

How does it know the data has changed and needs to invalidate the cache item? Wouldn’t it need to run the query to find out and thus defeat the purpose of caching in the first place!?

I am I better off using something like TagDependency instead so that when I change the data I can then just invalidate the cache myself?

Yes, it needs another query. The catch here is to replace expensive query with less expensive query.

Yes, that’s preferred unless you have too much cache and having trouble resetting it manually.

Great - thank you!