Working with "granular cache"
ScandiPWA controls cache in a different way than default Magento 2 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. 1.
    Make sure it implements the Magento\Framework\DataObject\IdentityInterface
1
use Magento\Framework\DataObject\IdentityInterface;
2
use Magento\Framework\Model\AbstractModel;
3
4
class Slide extends AbstractModel implements IdentityInterface {
5
public function getIdentities() {
6
// TODO: implement
7
}
8
}
Copied!
2. Specify the caching tag constant, it should be short and unique:
1
class Slide extends AbstractModel implements IdentityInterface {
2
const CACHE_TAG = 'sw_sld';
3
4
protected $_cacheTag = self::CACHE_TAG;
5
}
Copied!
3. Implement the getIdentities method, specify all involved cache identities. In our example, on slide save, the slider model should also be invalidate:
1
class Slide extends AbstractModel implements IdentityInterface {
2
const CACHE_TAG = 'sw_sld';
3
4
public function getIdentities() {
5
return [
6
self::CACHE_TAG . '_' . $this->getId(),
7
Slider::CACHE_TAG . '_' . $this->getSliderId()
8
];
9
}
10
}
Copied!
4. Add the names for events, prefer unique, descriptive names:
1
class Slide extends AbstractModel implements IdentityInterface {
2
protected $_eventPrefix = 'scandiweb_slider_slide';
3
}
Copied!
5. In case you have a resource model, i.e. the Collection, add the event after collection save:
1
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
2
3
class Collection extends AbstractCollection {
4
protected function _afterLoadData() {
5
parent::_afterLoadData();
6
7
$collection = clone $this;
8
9
if (count($collection)) {
10
$this->_eventManager->dispatch(
11
'scandiweb_slider_slider_collection_load_after',
12
['collection' => $collection]
13
);
14
}
15
16
return $this;
17
}
18
}
Copied!
6. It is finally time to connect the events, to granular cache management classes. Create, or modify the etc/events.xml with the following content:
1
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
2
<event name="scandiweb_slider_slider_collection_load_after">
3
<observer name="pq_cc_slider" instance="ScandiPWA\Cache\Observer\Response\TagEntityResponse"/>
4
</event>
5
<event name="scandiweb_slider_slider_save_after">
6
<observer name="pq_cc_slider" instance="ScandiPWA\Cache\Observer\FlushVarnishObserver"/>
7
</event>
8
<event name="scandiweb_slider_slide_collection_load_after">
9
<observer name="pq_cc_slide" instance="ScandiPWA\Cache\Observer\Response\TagEntityResponse"/>
10
</event>
11
<event name="scandiweb_slider_slide_save_after">
12
<observer name="pq_cc_slide" instance="ScandiPWA\Cache\Observer\FlushVarnishObserver"/>
13
</event>
14
</config>
Copied!
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.
Copy link
Contents
General rule