Skip to content

Instantly share code, notes, and snippets.

@skowron-line
Forked from kunicmarko20/Article.php
Created September 25, 2020 11:37
Show Gist options
  • Save skowron-line/49299b51398901cef6759cbab7e6b43d to your computer and use it in GitHub Desktop.
Save skowron-line/49299b51398901cef6759cbab7e6b43d to your computer and use it in GitHub Desktop.
Symfony Second Level Cache
<?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;
}
<?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;
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;
/**
* @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;
}
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:'
<?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;
/**
* @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;
}
snc_second_level_cache:
class: '%snc_redis.doctrine_cache_predis.class%'
arguments:
- '@snc_redis.default'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment