Message-ID: <125428160.4012.1485856334317.JavaMail.confluence@ip-10-127-227-164> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_4011_623413210.1485856334298" ------=_Part_4011_623413210.1485856334298 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html Smart HttpCache clearing

Smart HttpCache clearing

EZP >=3D 2015.01

Smart Http cache clearing refers to the ability to clea= r cache for locations/content that can be in relation with the content bein= g currently cleared.

When published, any content item usually has at least one location, figu= red by its URL. Therefore Http cache being bound to URLs, if content = A is updated (new version is published), we want Http cache for all = its locations to be cleared, so the content itself can appear up-to-date ev= erywhere it is supposed to be displayed. Sometimes, clearing cache for the = content's locations is not sufficient. You can for instance have an excerpt= of it displayed in a list from the parent location, or from within a relat= ion. In this case, cache for the parent location and/or the relation need t= o be cleared as well (at least if an ESI is not used).

The mechanism

Smart Http cache clearing is an event based mechanism. = Whenever a content item needs its cache to be cleared, the cache purger ser= vice sends an ezpublish.cache_clear.content event (also identi= fied by eZ\Publish\Core\MVC\Symfony\MVCEvents::CACHE_CLEAR_CONTENT constant) and passes a eZ\Publish\Core\MVC\Symfony\Event\Content= CacheClearEvent event object. This object contains the ContentInfo o= bject we need to clear the cache for. Every listener for this event can add= location objects to the cache clear list.

Once the event dispatched, the purger passes collected location objects = to the purge client, which will effectively send the cache BAN= request.

Note: The event is dispatched with a dedicated event dispa= tcher,=20 ezpublish.http_cache.event_dispatcher.

Default behavior

By default, following locations will be added to the cache clear list:

Implementing = a custom listener

By design, smart Http cache clearing is extensible. One can easily imple= ment an event listener/subscriber to the ezpublish.cache_clear.conten= t event and add locations to the cache clear list.

Example

Here's a very simple custom listener example, adding an arbitrary locati= on to the list.

Important: Cache clear listener services=20 must be tagged as=20 ezpublish.http_cache.event_subscriber or=20 ezpublish.http_cache.event_listener.
=20
namespace Acme\AcmeTestBundle\EventListener;

use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\Core\MVC\Symfony\Event\ContentCacheClearEvent;
use eZ\Publish\Core\MVC\Symfony\MVCEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class ArbitraryLocationsListener implements EventSubscriberInterface
{
    /**
     * @var LocationService
     */
    private $locationService;

    public function __construct( LocationService $locationService )
    {
        $this->locationService =3D $locationService;
    }

    public static function getSubscribedEvents()
    {
        return [MVCEvents::CACHE_CLEAR_CONTENT =3D> ['onContentCacheClea=
r', 100]];
    }

    public function onContentCacheClear( ContentCacheClearEvent $event )
    {
        // $contentInfo is the ContentInfo object for the content being cle=
ared.
        // You can extract information from it (e.g. ContentType from its c=
ontentTypeId), using appropriate Repository services.
        $contentInfo =3D $event->getContentInfo();

        // Adding arbitrary locations to the cache clear list.
        $event->addLocationToClear( $this->locationService->loadLo=
cation( 123 ) );
        $event->addLocationToClear( $this->locationService->loadLo=
cation( 456 ) );
    }
}
=20
=20
parameters:
    acme.cache_clear.arbitrary_locations_listener.class: Acme\AcmeTestBundl=
e\EventListener\ArbitraryLocationsListener

services:
    acme.cache_clear.arbitrary_locations_listener:
        class: %acme.cache_clear.arbitrary_locations_listener.class%
        arguments: [@ezpublish.api.service.location]
        tags:
            - { name: ezpublish.http_cache.event_subscriber }

=20
------=_Part_4011_623413210.1485856334298--