24. Caching

The Doctrine ORM package can leverage cache adapters implementing the PSR-6 standard to allow you to improve the performance of various aspects of Doctrine by simply making some additional configurations and method calls.

Doctrine ORM パッケージは、PSR-6 標準を実装するキャッシュ アダプターを利用して、追加の構成とメソッド呼び出しを行うだけで、Doctrine のさまざまな側面のパフォーマンスを向上させることができます。

24.1. Types of Caches

24.1.1. Query Cache

It is highly recommended that in a production environment you cache the transformation of a DQL query to its SQL counterpart. It doesn’t make sense to do this parsing multiple times as it doesn’t change unless you alter the DQL query.

本番環境では、DQL クエリから対応する SQL クエリへの変換をキャッシュすることを強くお勧めします。 DQL クエリを変更しない限り変更されないため、この解析を複数回実行しても意味がありません。

This can be done by configuring the query cache implementation to use on your ORM configuration.

これは、ORM 構成でクエリ キャッシュの実装を使用するように構成することで実行できます。

<?php
$cache = new \Symfony\Component\Cache\Adapter\PhpFilesAdapter('doctrine_queries');
$config = new \Doctrine\ORM\Configuration();
$config->setQueryCache($cache);

24.1.2. Result Cache

The result cache can be used to cache the results of your queries so that we don’t have to query the database again after the first time. You just need to configure the result cache implementation.

結果キャッシュを使用してクエリの結果をキャッシュできるため、初回以降はデータベースに再度クエリを実行する必要がありません。必要なのは、結果キャッシュの実装を構成することだけです。

<?php
$cache = new \Symfony\Component\Cache\Adapter\PhpFilesAdapter(
    'doctrine_results',
    0,
    '/path/to/writable/directory'
);
$config = new \Doctrine\ORM\Configuration();
$config->setResultCache($cache);

Now when you’re executing DQL queries you can configure them to use the result cache.

これで、DQL クエリを実行するときに、結果キャッシュを使用するように構成できます。

<?php
$query = $em->createQuery('select u from \Entities\User u');
$query->enableResultCache();

You can also configure an individual query to use a different result cache driver.

個々のクエリを構成して、異なる結果キャッシュ ドライバーを使用することもできます。

<?php
$cache = new \Symfony\Component\Cache\Adapter\PhpFilesAdapter(
    'doctrine_results',
    0,
    '/path/to/writable/directory'
);
$query->setResultCache($cache);

Note

ノート

Setting the result cache driver on the query will automatically enable the result cache for the query. If you want to disable it use disableResultCache().

クエリに結果キャッシュ ドライバーを設定すると、クエリの結果キャッシュが自動的に有効になります。無効にする場合は、disableResultCache() を使用します。

<?php
$query->disableResultCache();

If you want to set the time the cache has to live you can use the setResultCacheLifetime() method.

キャッシュの存続時間を設定したい場合は、setResultCacheLifetime() メソッドを使用できます。

<?php
$query->setResultCacheLifetime(3600);

The ID used to store the result set cache is a hash which is automatically generated for you if you don’t set a custom ID yourself with the setResultCacheId() method.

結果セットのキャッシュを保存するために使用される ID は、 setResultCacheId() メソッドでカスタム ID を自分で設定しない場合に自動的に生成されるハッシュです。

<?php
$query->setResultCacheId('my_custom_id');

You can also set the lifetime and cache ID by passing the values as the first and second argument to enableResultCache().

enableResultCache() の 1 番目と 2 番目の引数として値を渡すことで、有効期間とキャッシュ ID を設定することもできます。

<?php
$query->enableResultCache(3600, 'my_custom_id');

24.1.3. Metadata Cache

Your class metadata can be parsed from a few different sources like YAML, XML, Attributes, Annotations etc. Instead of parsing this information on each request we should cache it using one of the cache drivers.

クラスのメタデータは、YAML、XML、属性、注釈などのいくつかの異なるソースから解析できます。リクエストごとにこの情報を解析する代わりに、キャッシュドライバーの 1 つを使用してキャッシュする必要があります。

Just like the query and result cache we need to configure it first.

クエリと結果のキャッシュと同様に、最初に構成する必要があります。

<?php
$cache = \Symfony\Component\Cache\Adapter\PhpFilesAdapter(
    'doctrine_metadata',
    0,
    '/path/to/writable/directory'
);
$config = new \Doctrine\ORM\Configuration();
$config->setMetadataCache($cache);

Now the metadata information will only be parsed once and stored in the cache driver.

これで、メタデータ情報は一度だけ解析され、キャッシュ ドライバーに格納されます。

24.2. Clearing the Cache

We’ve already shown you how you can use the API of the cache drivers to manually delete cache entries. For your convenience we offer command line tasks to help you with clearing the query, result and metadata cache.

キャッシュ ドライバーの API を使用してキャッシュ エントリを手動で削除する方法については、既に説明しました。便宜上、クエリ、結果、およびメタデータのキャッシュをクリアするのに役立つコマンド ライン タスクを提供しています。

From the Doctrine command line you can run the following commands:

Doctrine コマンドラインから以下のコマンドを実行できます:

To clear the query cache use the orm:clear-cache:query task.

クエリ キャッシュをクリアするには、orm:clear-cache:query タスクを使用します。

$ ./doctrine orm:clear-cache:query

To clear the metadata cache use the orm:clear-cache:metadata task.

メタデータ キャッシュをクリアするには、orm:clear-cache:metadata タスクを使用します。

$ ./doctrine orm:clear-cache:metadata

To clear the result cache use the orm:clear-cache:result task.

結果キャッシュをクリアするには、orm:clear-cache:result タスクを使用します。

$ ./doctrine orm:clear-cache:result

All these tasks accept a --flush option to flush the entire contents of the cache instead of invalidating the entries.

これらのタスクはすべて --flush オプションを受け入れて、エントリを無効にする代わりにキャッシュの内容全体をフラッシュします。

Note

ノート

None of these tasks will work with APC, APCu, or XCache drivers because the memory that the cache is stored in is only accessible to the webserver.

これらのタスクはいずれも、APC、APCu、または XCache ドライバーでは機能しません。これは、キャッシュが格納されているメモリにアクセスできるのは Web サーバーのみであるためです。

24.3. Cache Chaining

A common pattern is to use a static cache to store data that is requested many times in a single PHP request. Even though this data may be stored in a fast memory cache, often that cache is over a network link leading to sizable network traffic.

一般的なパターンは、静的キャッシュを使用して、単一の PHP 要求で何度も要求されるデータを格納することです。このデータは高速なメモリ キャッシュに保存される場合がありますが、多くの場合、そのキャッシュはネットワーク リンク上にあり、かなりのネットワーク トラフィックが発生します。

A chain cache class allows multiple caches to be registered at once. For example, a per-request array cache can be used first, followed by a (relatively) slower Memcached cache if the array cache misses. The chain cache automatically handles pushing data up to faster caches in the chain and clearing data in the entire stack when it is deleted.

チェーン キャッシュ クラスを使用すると、複数のキャッシュを一度に登録できます。たとえば、リクエストごとの配列キャッシュを最初に使用し、配列キャッシュが見つからない場合は (比較的) 低速な Memcached キャッシュを使用できます。チェーン キャッシュは、データのプッシュを自動的に処理し、チェーン内のキャッシュを高速化し、削除時にスタック全体のデータをクリアします。

Symfony Cache provides such a chain cache. To find out how to use it, please have a look at the Symfony Documentation.

Symfony Cache はそのようなチェーン キャッシュを提供します。使用方法については、Symfony のドキュメントを参照してください。

24.4. Cache Slams

Something to be careful of when using the cache drivers is “cache slams”. Imagine you have a heavily trafficked website with some code that checks for the existence of a cache record and if it does not exist it generates the information and saves it to the cache. Now, if 100 requests were issued all at the same time and each one sees the cache does not exist and they all try to insert the same cache entry it could lock up APC, Xcache, etc. and cause problems. Ways exist to work around this, like pre-populating your cache and not letting your users’ requests populate the cache.

キャッシュドライバを使用する際の注意点は「キャッシュスラム」です。キャッシュ レコードの存在をチェックし、存在しない場合は情報を生成してキャッシュに保存するコードを含むトラフィックの多い Web サイトがあるとします。キャッシュが存在せず、すべてが同じキャッシュ エントリを挿入しようとするため、APC や Xcache などがロックされ、問題が発生する可能性があります。これを回避するには、キャッシュを事前に設定し、ユーザーの要求がキャッシュに設定されないようにするなどの方法があります。

You can read more about cache slams in this blog post.

キャッシュ スラムの詳細については、このブログ投稿を参照してください。

Table Of Contents

Previous topic

23. PHP Mapping

23. PHP マッピング

Next topic

25. Improving Performance

25. パフォーマンスの向上

This Page

Fork me on GitHub