|
| 1 | +Caching |
| 2 | +======= |
| 3 | + |
| 4 | +Caching is very important in Doctrine. |
| 5 | + |
| 6 | +In this example for Metadata, Queries, and Results we set an array |
| 7 | +cache for the result\_cache. Please note the array cache is for |
| 8 | +development only and shown here along side other cache types. |
| 9 | + |
| 10 | +If you want to set a cache for query, result and metadata, you can |
| 11 | +specify this inside your ``config/autoload/local.php`` |
| 12 | + |
| 13 | +.. code:: php |
| 14 | +
|
| 15 | + return [ |
| 16 | + 'doctrine' => [ |
| 17 | + 'configuration' => [ |
| 18 | + 'orm_default' => [ |
| 19 | + 'query_cache' => 'filesystem', |
| 20 | + 'result_cache' => 'array', |
| 21 | + 'metadata_cache' => 'apc', |
| 22 | + 'hydration_cache' => 'memcached', |
| 23 | + ], |
| 24 | + ], |
| 25 | + ], |
| 26 | + ]; |
| 27 | +
|
| 28 | +The previous configuration takes into consideration different cache |
| 29 | +adapters. You can specify any other adapter that implements the |
| 30 | +``Doctrine\Common\Cache\Cache`` interface. Find more |
| 31 | +`here <https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/caching.html>`__. |
| 32 | + |
| 33 | + |
| 34 | +Redis Example |
| 35 | +------------- |
| 36 | + |
| 37 | +This example uses a factory to create the Redis cache object. The Redis install used here |
| 38 | +can be found at `https://github.com/phpredis/phpredis#class-redis <https://github.com/phpredis/phpredis#class-redis>`__ |
| 39 | +See also `https://redislabs.com/lp/php-redis/ <https://redislabs.com/lp/php-redis/>`__ |
| 40 | + |
| 41 | +module.config.php |
| 42 | + |
| 43 | +.. code:: php |
| 44 | +
|
| 45 | + namespace Db; |
| 46 | +
|
| 47 | + return [ |
| 48 | + 'service_manager' => [ |
| 49 | + 'factories' => [ |
| 50 | + 'Db\Cache\Redis' => Db\Cache\RedisFactory::class, |
| 51 | + ], |
| 52 | + ], |
| 53 | + 'doctrine' => [ |
| 54 | + 'cache' => [ |
| 55 | + 'redis' => [ |
| 56 | + 'namespace' => 'Db_Doctrine', |
| 57 | + 'instance' => 'Db\Cache\Redis', |
| 58 | + ], |
| 59 | + ], |
| 60 | + 'configuration' => [ |
| 61 | + 'orm_default' => [ |
| 62 | + 'query_cache' => 'redis', |
| 63 | + 'result_cache' => 'redis', |
| 64 | + 'metadata_cache' => 'redis', |
| 65 | + 'hydration_cache' => 'redis', |
| 66 | + ], |
| 67 | + ], |
| 68 | + ], |
| 69 | + ]; |
| 70 | +
|
| 71 | +
|
| 72 | +Db\\Cache\\RedisFactory |
| 73 | + |
| 74 | +.. code:: php |
| 75 | +
|
| 76 | + namespace Db\Cache; |
| 77 | +
|
| 78 | + use Interop\Container\ContainerInterface; |
| 79 | + use Redis; |
| 80 | +
|
| 81 | + class RedisFactory |
| 82 | + { |
| 83 | + public function __invoke( |
| 84 | + ContainerInterface $container, |
| 85 | + $requestedName, |
| 86 | + array $options = null |
| 87 | + ) { |
| 88 | + $redis = new Redis(); |
| 89 | + $redis->connect('127.0.0.1', 6379); |
| 90 | +
|
| 91 | + return $redis; |
| 92 | + } |
| 93 | + } |
| 94 | +
|
| 95 | +
|
| 96 | +Read more about |
| 97 | +`Caching <https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/caching.html>`__. |
| 98 | + |
| 99 | + |
| 100 | +How to enable and configure Second Level Cache |
| 101 | +---------------------------------------------- |
| 102 | + |
| 103 | +.. code:: php |
| 104 | +
|
| 105 | + return [ |
| 106 | + 'doctrine' => [ |
| 107 | + 'configuration' => [ |
| 108 | + 'orm_default' => [ |
| 109 | + 'result_cache' => 'redis', // Second level cache reuse the cache defined in result cache |
| 110 | + 'second_level_cache' => [ |
| 111 | + 'enabled' => true, |
| 112 | + 'default_lifetime' => 200, |
| 113 | + 'default_lock_lifetime' => 500, |
| 114 | + 'file_lock_region_directory' => __DIR__ . '/../my_dir', |
| 115 | + 'regions' => [ |
| 116 | + 'My\FirstRegion\Name' => [ |
| 117 | + 'lifetime' => 800, |
| 118 | + 'lock_lifetime' => 1000, |
| 119 | + ], |
| 120 | + 'My\SecondRegion\Name' => [ |
| 121 | + 'lifetime' => 10, |
| 122 | + 'lock_lifetime' => 20, |
| 123 | + ], |
| 124 | + ], |
| 125 | + ], |
| 126 | + ], |
| 127 | + ], |
| 128 | + ], |
| 129 | + ]; |
| 130 | +
|
| 131 | +You also need to add the ``Cache`` annotation to your model (`read |
| 132 | +more <https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/second-level-cache.html#entity-cache-definition>`__). |
| 133 | +Read more about `Second Level |
| 134 | +Cache <http://docs.doctrine-project.org/projects/doctrine-orm/en/current/reference/second-level-cache.html>`__. |
0 commit comments