21

I see that a lot of the models in Magento 2 implement Magento\Framework\DataObject\IdentityInterface.
This interface has a single method called getIdentities
The implementations of the method usually return return [self::CACHE_TAG . '_' . $this->getId()];.

An example can be found here
What is this used for?

6
  • I don't really know if that is used but the class is present in the dev/tests/static/testsuite/Magento/Test/Legacy/_files/obsolete_classes.php file so maybe it is intended to be removed later. Apr 22 '16 at 12:05
  • 2
    Judging by the class comment it is used for cache and make a unique entity ID and used in ESI headers vendor/magento/module-page-cache/Controller/Block/Esi.php line 28 Apr 22 '16 at 12:09
  • @MatthéoGeoffray yes you're right but the getIdentities method is called on a block class in this case, the OP is asking about the models classes Apr 22 '16 at 12:10
  • Oh yes right my bad ;) Apr 22 '16 at 12:14
  • @MatthéoGeoffray. I think you are right. You might want to put your comment as an answer.
    – Marius
    Apr 22 '16 at 12:15
27

From what I understood, this is kind of the equivalence of the getCacheTags method of Magento 1.

The getIdentities from model classes is then used in every block class referencing this model.

Ok let's take the /Magento/Catalog/Model/Category.php:

public function getIdentities()
{
    $identities = [
        self::CACHE_TAG . '_' . $this->getId(),
    ];
    if ($this->hasDataChanges() || $this->isDeleted()) {
        $identities[] = Product::CACHE_PRODUCT_CATEGORY_TAG . '_' . $this->getId();
    }
    return $identities;
}

This method is then referenced in /Magento/Catalog/Block/Category/View.php:

public function getIdentities()
{
    return $this->getCurrentCategory()->getIdentities();
}

In M2, you now have to declare the cache tag with the getIdentities method at the model level, then you can use it in blocks referencing those models.

If you check every block implementing the getIdentities method, they all reference a corresponding model getIdentities method or a corresponding model cache tag such as \Magento\Catalog\Model\Product::CACHE_TAG

Then those block getIdentities methods are used in Varnish for caching reasons as mentioned by Matthéo to set the X-Magento-Tags header.

This header is then used in Magento/Framework/App/PageCache/Kernel.php in the process() method to save the cache:

$tagsHeader = $response->getHeader('X-Magento-Tags');
$tags = $tagsHeader ? explode(',', $tagsHeader->getFieldValue()) : [];

$response->clearHeader('Set-Cookie');
$response->clearHeader('X-Magento-Tags');
if (!headers_sent()) {
    header_remove('Set-Cookie');
}
$this->cache->save(serialize($response), $this->identifier->getValue(), $tags, $maxAge);
3
  • 6
    Be careful - identities are just for the full page cache. For the block cache you still need to implement cache_tags and cache_lifetime! It's an addition, not a replacement. Jan 20 '17 at 15:52
  • I wanted to keep FPC in a page with a dynamic block so I had to implement getIdentities in the block, but the model (Slider in my case) depends also on changes in child Model (Banners), should I add both in the identities array ? or the child change means only the parent is needed ? thank you @RobertEgginton
    – medmek
    Nov 12 '19 at 18:51
  • @RobertEgginton I used CACHE_TAG using getIdentities() in Model and due to this my extension is failed in Varnish PageCache Test, Here is the screenshot: prnt.sc/1rzivjt Can you please guide me? Sep 12 at 22:09
6

Judging by the class Magento\Framework\DataObject\IdentityInterface comment it is used for cache and make a unique entity ID that is used in Varnish ESI headers in vendor/magento/module-page-cache/Controller/Block/Esi.phpline 28.

if ($blockInstance instanceof \Magento\Framework\DataObject\IdentityInterface) {
   $response->setHeader('X-Magento-Tags', implode(',', $blockInstance->getIdentities()));
}
5

The  IdentityInterface  will  force  Model  class  to  define  the  ​getIdentities()  method which  will return a unique id for the model. You must only use this interface if your model required cache refresh after database operation and render information to the frontend page.

1
  • Thank you. If you could link to the place in the documentation or other good source, then this would be the most useful answer on this page.
    – dotancohen
    Aug 16 '20 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.