PHP code example of acpl / flarum-lscache

1. Go to this page and download the library: Download acpl/flarum-lscache library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

acpl / flarum-lscache example snippets


(new Extend\Routes('api'))
    ->get('/examples', 'examples.index', ExamplesListController::class)
    ->post('/examples', 'examples.create', ExamplesCreateController::class)
    // and so on

// 💡 resource name should be in singular form
\ACPL\FlarumLSCache\Utility\LSCachePurger::$resourcesSupportedByEvent[] = 'example'

return [
    // ... your current extenders
];

// extend.php
use Flarum\Extend;

return [
    // ... your current extenders
    (new Extend\Conditional)
        ->whenExtensionEnabled('acpl-lscache', [
            (new Extend\Event)->listen(ExampleUpdated::class, ExampleUpdatedListener::class)
        ]),
];

// ExampleUpdatedListener.php
use ACPL\FlarumLSCache\Listener\AbstractCachePurgeListener;

class ExampleUpdatedListener extends AbstractCachePurgeListener
{
    /** @param  ExampleUpdated  $event */
    protected function addPurgeData($event): void  
    {
        // Purge cache tag
        $this->purger->addPurgeTag('examples');
        // or purge multiple cache tags
        $this->purger->addPurgeTags([
            'examples',
            "examples_{$event->example->id}"
        ]);

        // Purge a single path
        $this->purger->addPurgePath('/examples');
        // or purge multiple paths
        $this->purger->addPurgePaths([
            '/examples',
            "/examples_{$event->example->id}",
        ]);
    }
}

// extend.php
use Flarum\Extend;

return [
    // ... your current extenders
    (new Extend\Conditional)
        ->whenExtensionEnabled('acpl-lscache', [
            (new Extend\Event)->subscribe(ExampleEventSubscriber::class),
        ]),
];

// ExampleEventSubscriber.php
use ACPL\FlarumLSCache\Listener\AbstractCachePurgeSubscriber;
use Illuminate\Contracts\Events\Dispatcher;

class ExampleEventSubscriber extends AbstractCachePurgeSubscriber
{
    public function subscribe(Dispatcher $events): void
    {
        $this->addPurgeListener($events, ExampleUpdated::class, [$this, 'handleExampleUpdated']);
        // ... rest of listeners
    }

    public function handleExampleUpdated(ExampleUpdated $event): void
    {
        $this->purger->addPurgeTags([
            'examples',
            "example_{$event->example->id}",
        ]);
    }
    // ... rest of methods
}
sh
composer update acpl/flarum-lscache
php flarum migrate
php flarum cache:clear