Implementing Caching for New Caching Identities

ScandiPWA controls cache in a different way than default Magento 2arrow-up-right does. We are still using the caching identities, but, instead of specifying them on GraphQL queries we use events.

General rule

To make some of your model work as cache identity manager:

  1. Make sure it implements the Magento\Framework\DataObject\IdentityInterface

use Magento\Framework\DataObject\IdentityInterface;
use Magento\Framework\Model\AbstractModel;

class Slide extends AbstractModel implements IdentityInterface {
    public function getIdentities() {
        // TODO: implement
    }
}

2. Specify the caching tag constant, it should be short and unique:

class Slide extends AbstractModel implements IdentityInterface {
    const CACHE_TAG = 'sw_sld';

    protected $_cacheTag = self::CACHE_TAG;
}

3. Implement the getIdentities method, specify all involved cache identities. In our example, on slide save, the slider model should also be invalidate:

4. Add the names for events, prefer unique, descriptive names:

5. In case you have a resource model, i.e. the Collection, add the event after collection save:

6. It is finally time to connect the events, to granular cache management classes. Create, or modify the etc/events.xml with following content:

Note, there are two classes used as observers:

  • ScandiPWA\Cache\Observer\FlushVarnishObserver - responsible for flushing, must be triggered on save of the model.

  • ScandiPWA\Cache\Observer\Response\TagEntityResponse - responsible for tagging, must be triggered after load of the collection / model.

Last updated