3

What is the difference between cache_key and cache_tags in Magento?

protected function _construct() { $this->addData(array( 'cache_lifetime' => 120, 'cache_tags' => array(Mage_Catalog_Model_Product::CACHE_TAG . "_" . $this->getProduct()->getId()), 'cache_key' => $this->getProduct()->getId(), )); } 

Both seem to be unique (applying to only one output). I understand that cache_tags relate to cache types in backend, but why does it have to be unique if we already have cache_key being unique?

Will having a generic cache_tags cause all block instances to have the same output?

Thanks

2
  • 1
    I fail to see any use case where setting the product id in both keys would give some advantage. Never seen such usage in standard Magento, must be some custom stuff. Maybe it's just a typo and the author wanted to append the store_id to cache_tags. Commented May 10, 2013 at 15:34
  • it does make sense, magento has a cache key for each product/model, and you can have multiple caches for a single product, e.g. details view, quick view, review,... when you change/save a product this above tag gets actually cleared by magento. See Mage_Core_Model_Abstract::getCacheIdTags() Commented May 10, 2013 at 17:31

3 Answers 3

1

Cache Tags don't have to be unique. In your example they look unique by having the ID in it, but there could be other things attached to this tag as well. The advantage is, if you do a change for that specific product, you can clear all cache-entries that are associated to this product (as long as they have this "unique" tag specified)

Sign up to request clarification or add additional context in comments.

2 Comments

I'm confused by the comment on this page: magentocommerce.com/wiki/5_-_modules_and_development/… Which says: "This code is OK if and only if the output does not depend of a specific product. If you are doing so for the product view, all your products will have the same output !" So basically, identical cache_tags for different block instances will not cause them to have same output?
its a bit misleading example 1, even so no cache_key is defined it actually has one. A cache-key is required. See Mage_Core_Block_Template::getCacheKeyInfo() and Mage_Core_Block_Abstract::getCacheKey(). The problem with that "default" cache key is that it will always returns the same cached data, no matter what product is assigned.
1

Within one Cache tag you can store cache data with different cache Key.

e.g cache for store1 and cache for store 2 will be differentiated by cache Key: 'cache_key' = storeId // some psuedo code statement not actual code

when Cache is to be cleaned it can be done using CACHE TAG or Cache tag plus cache Key info.

Comments

1

Cache id (cache_key) is used to identify a specific cache record. If you save something in the cache you can later retrieve it by its id (cache_key || cache id).

Cache tags (cache_tags) are a way to assign labels to different cache records so you can later clear multiple cache entries based on those labels (tags).

For example, let's say you want to cache a category view page. This will have as tags category_{id of category here}. But the category page contains products. SO maybe you want to also add the tags product_{id of each product here}.

Now, when you change a product and/or a category you need to clean the cache for each entity involved. So when you update a product you clear all cache entries with the tag product_{id of product here}.

This way, the category page cache I mentioned above will get cleared also.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.