ScandiPWA (deprecated)
User ManualGitHubSlack
  • ScandiPWA (deprecated)
  • React & PWA for dummies
    • Setting Up Environment and Talking Theory
    • Learning React Hands-on
    • Styling It
    • Learning ScandiPWA Way
  • START & UPGRADE
    • Linux Docker setup
    • Mac Docker setup
    • Setting up the theme with remote M2 server
    • Theme Upgrade to the latest version
    • Automated setup (BETA)
  • A TO Z OVERVIEW
    • Motivation
    • Challenges
    • File structure and UI components
    • Rewriting and Extending A Theme
  • DESCRIPTION OF CONTAINERS
    • Ngrok
  • FAQ
    • Development
    • Installation
    • Billing and license
    • Product support
    • What is PWA?
  • HOW-TO TUTORIALS - INTRODUCTORY
    • Base template
    • Connecting to the GraphQL resolver
    • Extension mechanism
    • Creating GraphQL resolver
    • Debugging and Inspecting
    • Setting Up Frontend
    • File Structure
    • Data Flow
    • Technology Stack
    • Changing environment
    • Theme Build and Configuration
    • Implementing Caching for New Caching Identities
    • Implementing a parent theme
  • HOW-TO TUTORIALS - INTERMEDIATE
    • Debugging in Chrome
    • Configuring XDebug
    • CLI in Docker
    • Postman & GraphQL Playground
    • VSCode Extensions
    • Plugins: implementing
    • Plugins: using and publishing
    • ESlint & StyleLint
    • How To Contribute
    • Migrating to a Newer Version
    • Installation on Existing Magento 2 Sever
    • BEM and Coding Standards
    • Tools of ScandiPWA
    • React Best Practices
  • FAQ
    • Untitled
Powered by GitBook
On this page
  1. HOW-TO TUTORIALS - INTRODUCTORY

Implementing Caching for New Caching Identities

PreviousTheme Build and ConfigurationNextImplementing a parent theme

Last updated 4 years ago

ScandiPWA controls cache in a different way than 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:

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

    public function getIdentities() {
        return [
            self::CACHE_TAG . '_' . $this->getId(),
            Slider::CACHE_TAG . '_' . $this->getSliderId()
        ];
    }
}

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

class Slide extends AbstractModel implements IdentityInterface {
    protected $_eventPrefix = 'scandiweb_slider_slide';
}

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

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

class Collection extends AbstractCollection {
    protected function _afterLoadData() {
        parent::_afterLoadData();

        $collection = clone $this;

        if (count($collection)) {
            $this->_eventManager->dispatch(
                'scandiweb_slider_slider_collection_load_after',
                ['collection' => $collection]
            );
        }

        return $this;
    }
}

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

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="scandiweb_slider_slider_collection_load_after">
        <observer name="pq_cc_slider" instance="ScandiPWA\Cache\Observer\Response\TagEntityResponse"/>
    </event>
    <event name="scandiweb_slider_slider_save_after">
        <observer name="pq_cc_slider" instance="ScandiPWA\Cache\Observer\FlushVarnishObserver"/>
    </event>
    <event name="scandiweb_slider_slide_collection_load_after">
        <observer name="pq_cc_slide" instance="ScandiPWA\Cache\Observer\Response\TagEntityResponse"/>
    </event>
    <event name="scandiweb_slider_slide_save_after">
        <observer name="pq_cc_slide" instance="ScandiPWA\Cache\Observer\FlushVarnishObserver"/>
    </event>
</config>

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.

default Magento 2