Merge pull request #1279 from phansys/doc-slc
[Doc][Reference][2nd level cache]
This commit is contained in:
commit
22533042a5
@ -113,8 +113,8 @@ Defines contract for concurrently managed data region.
|
|||||||
|
|
||||||
`See API Doc <http://www.doctrine-project.org/api/orm/2.5/class-Doctrine.ORM.Cache.ConcurrentRegion.html/>`_.
|
`See API Doc <http://www.doctrine-project.org/api/orm/2.5/class-Doctrine.ORM.Cache.ConcurrentRegion.html/>`_.
|
||||||
|
|
||||||
Cache region
|
Timestamp region
|
||||||
~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
``Doctrine\ORM\Cache\TimestampRegion``
|
``Doctrine\ORM\Cache\TimestampRegion``
|
||||||
|
|
||||||
@ -183,16 +183,15 @@ To enable the second-level-cache, you should provide a cache factory
|
|||||||
.. code-block:: php
|
.. code-block:: php
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
|
/* @var $config \Doctrine\ORM\Cache\RegionsConfiguration */
|
||||||
/* var $config \Doctrine\ORM\Cache\RegionsConfiguration */
|
/* @var $cache \Doctrine\Common\Cache\Cache */
|
||||||
/* var $cache \Doctrine\Common\Cache\Cache */
|
|
||||||
|
|
||||||
$factory = new \Doctrine\ORM\Cache\DefaultCacheFactory($config, $cache);
|
$factory = new \Doctrine\ORM\Cache\DefaultCacheFactory($config, $cache);
|
||||||
|
|
||||||
//Enable second-level-cache
|
// Enable second-level-cache
|
||||||
$config->setSecondLevelCacheEnabled();
|
$config->setSecondLevelCacheEnabled();
|
||||||
|
|
||||||
//Cache factory
|
// Cache factory
|
||||||
$config->getSecondLevelCacheConfiguration()
|
$config->getSecondLevelCacheConfiguration()
|
||||||
->setCacheFactory($factory);
|
->setCacheFactory($factory);
|
||||||
|
|
||||||
@ -220,13 +219,12 @@ To specify a default lifetime for all regions or specify a different lifetime fo
|
|||||||
.. code-block:: php
|
.. code-block:: php
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
|
/* @var $config \Doctrine\ORM\Configuration */
|
||||||
/* var $config \Doctrine\ORM\Configuration */
|
/* @var $cacheConfig \Doctrine\ORM\Configuration */
|
||||||
/* var $cacheConfig \Doctrine\ORM\Configuration */
|
|
||||||
$cacheConfig = $config->getSecondLevelCacheConfiguration();
|
$cacheConfig = $config->getSecondLevelCacheConfiguration();
|
||||||
$regionConfig = $cacheConfig->getRegionsConfiguration();
|
$regionConfig = $cacheConfig->getRegionsConfiguration();
|
||||||
|
|
||||||
//Cache Region lifetime
|
// Cache Region lifetime
|
||||||
$regionConfig->setLifetime('my_entity_region', 3600); // Time to live for a specific region; In seconds
|
$regionConfig->setLifetime('my_entity_region', 3600); // Time to live for a specific region; In seconds
|
||||||
$regionConfig->setDefaultLifetime(7200); // Default time to live; In seconds
|
$regionConfig->setDefaultLifetime(7200); // Default time to live; In seconds
|
||||||
|
|
||||||
@ -240,11 +238,10 @@ By providing a cache logger you should be able to get information about all cach
|
|||||||
.. code-block:: php
|
.. code-block:: php
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
|
/* @var $config \Doctrine\ORM\Configuration */
|
||||||
/* var $config \Doctrine\ORM\Configuration */
|
|
||||||
$logger = \Doctrine\ORM\Cache\Logging\StatisticsCacheLogger();
|
$logger = \Doctrine\ORM\Cache\Logging\StatisticsCacheLogger();
|
||||||
|
|
||||||
//Cache logger
|
// Cache logger
|
||||||
$config->setSecondLevelCacheEnabled(true);
|
$config->setSecondLevelCacheEnabled(true);
|
||||||
$config->getSecondLevelCacheConfiguration()
|
$config->getSecondLevelCacheConfiguration()
|
||||||
->setCacheLogger($logger);
|
->setCacheLogger($logger);
|
||||||
@ -457,7 +454,6 @@ Basic entity cache
|
|||||||
.. code-block:: php
|
.. code-block:: php
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
$em->persist(new Country($name));
|
$em->persist(new Country($name));
|
||||||
$em->flush(); // Hit database to insert the row and put into cache
|
$em->flush(); // Hit database to insert the row and put into cache
|
||||||
|
|
||||||
@ -480,7 +476,6 @@ Association cache
|
|||||||
.. code-block:: php
|
.. code-block:: php
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
// Hit database to insert the row and put into cache
|
// Hit database to insert the row and put into cache
|
||||||
$em->persist(new State($name, $country));
|
$em->persist(new State($name, $country));
|
||||||
$em->flush();
|
$em->flush();
|
||||||
@ -543,20 +538,19 @@ The query cache stores the results of the query but as identifiers, entity value
|
|||||||
.. code-block:: php
|
.. code-block:: php
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
|
/* @var $em \Doctrine\ORM\EntityManager */
|
||||||
|
|
||||||
/* var $em \Doctrine\ORM\EntityManager */
|
// Execute database query, store query cache and entity cache
|
||||||
|
$result1 = $em->createQuery('SELECT c FROM Country c ORDER BY c.name')
|
||||||
|
->setCacheable(true)
|
||||||
|
->getResult();
|
||||||
|
|
||||||
// Execute database query, store query cache and entity cache
|
$em->clear()
|
||||||
$result1 = $em->createQuery('SELECT c FROM Country c ORDER BY c.name')
|
|
||||||
->setCacheable(true)
|
|
||||||
->getResult();
|
|
||||||
|
|
||||||
$em->clear()
|
// Check if query result is valid and load entities from cache
|
||||||
|
$result2 = $em->createQuery('SELECT c FROM Country c ORDER BY c.name')
|
||||||
// Check if query result is valid and load entities from cache
|
->setCacheable(true)
|
||||||
$result2 = $em->createQuery('SELECT c FROM Country c ORDER BY c.name')
|
->getResult();
|
||||||
->setCacheable(true)
|
|
||||||
->getResult();
|
|
||||||
|
|
||||||
Cache mode
|
Cache mode
|
||||||
~~~~~~~~~~
|
~~~~~~~~~~
|
||||||
@ -571,13 +565,12 @@ The Cache Mode controls how a particular query interacts with the second-level c
|
|||||||
.. code-block:: php
|
.. code-block:: php
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
|
/* @var $em \Doctrine\ORM\EntityManager */
|
||||||
/** var $em \Doctrine\ORM\EntityManager */
|
// Will refresh the query cache and all entities the cache as it reads from the database.
|
||||||
// Will refresh the query cache and all entities the cache as it reads from the database.
|
$result1 = $em->createQuery('SELECT c FROM Country c ORDER BY c.name')
|
||||||
$result1 = $em->createQuery('SELECT c FROM Country c ORDER BY c.name')
|
->setCacheMode(Cache::MODE_GET)
|
||||||
->setCacheMode(Cache::MODE_GET)
|
->setCacheable(true)
|
||||||
->setCacheable(true)
|
->getResult();
|
||||||
->getResult();
|
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
@ -636,7 +629,6 @@ Using the last update timestamps as part of the query key invalidate the cache k
|
|||||||
.. code-block:: php
|
.. code-block:: php
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
// load from database and store cache query key hashing the query + parameters + last timestamp cache region..
|
// load from database and store cache query key hashing the query + parameters + last timestamp cache region..
|
||||||
$entities = $em->getRepository('Entity\Country')->findAll();
|
$entities = $em->getRepository('Entity\Country')->findAll();
|
||||||
|
|
||||||
@ -661,8 +653,7 @@ However, you can use the cache API to check / invalidate cache entries.
|
|||||||
.. code-block:: php
|
.. code-block:: php
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
|
/* @var $cache \Doctrine\ORM\Cache */
|
||||||
/* var $cache \Doctrine\ORM\Cache */
|
|
||||||
$cache = $em->getCache();
|
$cache = $em->getCache();
|
||||||
|
|
||||||
$cache->containsEntity('Entity\State', 1) // Check if the cache exists
|
$cache->containsEntity('Entity\State', 1) // Check if the cache exists
|
||||||
|
Loading…
Reference in New Issue
Block a user