PHP code example of lbarulski / cache-tags-bundle

1. Go to this page and download the library: Download lbarulski/cache-tags-bundle 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/ */

    

lbarulski / cache-tags-bundle example snippets


// app/ApplicationKernel.php
public function registerBundles()
{
    return array(
        // ...
        new lbarulski\CacheTagsBundle\CacheTagsBundle(),
        // ...
    );
}

// Acme\MainBundle\Controller\ArticleController.php

use lbarulski\CacheTagsBundle\Annotation\CacheTag\Plain;
...

/**
 * @CacheTag\Plain("article_name")
 **/
public function articleAction(Request $request)
{
    ...
    
    $response = new Response('...');
    $response->setPublic();
    $response->setTtl(3600);
    
    return $response;
}

// Acme\MainBundle\Entity\Article.php

use lbarulski\CacheTagsBundle\Tag\CacheTagInterface;

class Article implements CacheTagInterface
{
    ...
    
    public function getCacheTag()
	{
		return 'article_'.$this->getId();
	}
}

// Acme\MainBundle\Controller\ArticleController.php

use lbarulski\CacheTagsBundle\Annotation\CacheTag\RequestAttribute;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
...

/**
 * @ParamConverter("article")
 * @CacheTag\RequestAttribute("article")
 **/
public function articleAction(Article $article)
{
    ...
    
    $response = new Response('...');
    $response->setPublic();
    $response->setTtl(3600);
    
    return $response;
}

// Acme\MainBundle\Controller\ArticleController.php

use lbarulski\CacheTagsBundle\Tag\Plain;
...

public function updateArticleAction(Article $article)
{
    ...
    $tag = 'article_name';
    $this->get('cache_tags.invalidator')->invalidate(new Plain($tag));
    ...
}