ikucheriavenko
1/25/2018 - 9:28 PM

Symfony Second Level Cache

Symfony Second Level Cache

snc_second_level_cache:
    class: '%snc_redis.doctrine_cache_predis.class%'
    arguments:
        - '@snc_redis.default'
doctrine:
    #...
    orm:
        #...
        entity_managers:
            default:
                #...
                second_level_cache:
                    region_cache_driver:
                        type: service
                        id: snc_second_level_cache
                    enabled: true
                    region_lifetime: 86400

snc_redis:
    clients:
        default:
            type: predis
            alias: default
            dsn: redis://localhost
            logging: '%kernel.debug%'
        doctrine:
            type: predis
            alias: doctrine
            dsn: redis://localhost
            logging: '%kernel.debug%'

    doctrine:
        metadata_cache:
            client: doctrine
            entity_manager: default
            namespace: 'dmc:'
        result_cache:
            client: doctrine
            entity_manager: default
            namespace: 'drc:'
        query_cache:
            client: doctrine
            entity_manager: default
            namespace: 'dqc:'
        second_level_cache:
            client: doctrine
            entity_manager: default
            namespace: 'dslc:'

Symfony Second Level Cache

This example will help you enable Second Level Cache in Symfony with Redis, also there is an example for Doctrine Extensions Translatable and Doctrine Behaviours Translatable.

Libraries we need:

composer require doctrine/doctrine-bundle doctrine/orm snc/redis-bundle predis/predis
<?php

namespace AppBundle\Entity;

/**
 * @ORM\Entity
 * @ORM\Table(name="article_translations",
 *     uniqueConstraints={@ORM\UniqueConstraint(name="lookup_unique_article_idx", columns={
 *         "locale", "object_id", "field"
 *     })}
 * )
 * @ORM\Cache(usage="NONSTRICT_READ_WRITE")
 */
class ExtensionsArticleTranslation extends AbstractPersonalTranslation
{
    /**
     * @ORM\ManyToOne(targetEntity="ExtensionsArticle", inversedBy="translations")
     * @ORM\JoinColumn(name="object_id", referencedColumnName="id", onDelete="CASCADE")
     */
    protected $object;
}
<?php

namespace AppBundle\Entity;

/**
 * @package AppBundle\Entity
 * @ORM\Entity()
 * @ORM\Table()
 * @Gedmo\TranslationEntity(class="ExtensionsArticleTranslation")
 * @ORM\Cache(usage="NONSTRICT_READ_WRITE")
 */
class ExtensionsArticle implements TranslatableInterface
{
    use PersonalTranslatableTrait;
    /**
     * @var string
     * @ORM\Column(type="string")
     * @Gedmo\Translatable
     */
    protected $name;
  
     /**
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="article")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     * @ORM\Cache(usage="NONSTRICT_READ_WRITE")
     */
    protected $category;
  
      /**
     * @var ArrayCollection
     *
     * @ORM\OneToMany(
     *     targetEntity="ExtensionsArticleTranslation",
     *     mappedBy="object",
     *     cascade={"persist", "remove"}
     * )
     * @ORM\Cache(usage="NONSTRICT_READ_WRITE")
     */
    protected $translations;
}
<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @package AppBundle\Entity
 * @ORM\Entity()
 * @ORM\Table()
 * @ORM\Cache(usage="NONSTRICT_READ_WRITE")
 */
class Category
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="string", nullable=false)
     */
    private $name;
}
<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;

/**
 *
 * @ORM\Table()
 * @ORM\Entity
 * @ORM\Cache(usage="NONSTRICT_READ_WRITE")
 */
class BehaviorsArticleTranslation
{
    use ORMBehaviors\Translatable\Translation;

    /**
     * @ORM\Column(type="string")
     */
    protected $name;
}
<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;

/**
 * @package AppBundle\Entity
 * @ORM\Entity()
 * @ORM\Table()
 * @ORM\Cache(usage="NONSTRICT_READ_WRITE")
 */
class BehaviorsArticle implements TranslatableInterface
{
    use ORMBehaviors\Translatable\Translatable;
    /**
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="article")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     * @ORM\Cache(usage="NONSTRICT_READ_WRITE")
     */
    protected $category;
  
    /**
     * @ORM\OneToMany(
     *     targetEntity="BehaviorsArticleTranslation",
     *     orphanRemoval=true,
     *     mappedBy="translatable",
     *     indexBy="locale",
     *     cascade={"persist", "merge", "remove"}
     *)
     * @ORM\Cache(usage="NONSTRICT_READ_WRITE")
     */
    protected $translations;
}
<?php

namespace AppBundle\Entity;

/**
 * @package AppBundle\Entity
 * @ORM\Entity()
 * @ORM\Table()
 * @ORM\Cache(usage="NONSTRICT_READ_WRITE")
 */
class Article 
{
 
    /**
     * @ORM\Column(type="string")
     */
    protected $name;
  
    /**
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="article")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     * @ORM\Cache(usage="NONSTRICT_READ_WRITE")
     */
    protected $category;
}