設定Configuration
Laravelは多くのキャッシュシステムに統一したAPIを提供しています。キャッシュの設定はconfig/cache.phpで設定します。アプリケーション全体でデフォルトとして使用するキャッシュドライバーをこのファイルの中で指定します。MemcachedやRedisなど、人気のあるキャッシュシステムをLaravelは最初からサポートしています。Laravel provides a unified API for various caching systems. The cache configuration is located at config/cache.php. In this file you may specify which cache driver you would like used by default throughout your application. Laravel supports popular caching backends like Memcached[http://memcached.org] and Redis[http://redis.io] out of the box.
キャッシュ設定ファイルは、様々な他のオプションを含んでいます。コメントで説明してありますので、よく読んで確認してください。Laravelのデフォルトでは、fileキャッシュドライバーが設定されています。ファイルシステムにオブジェクトをシリアライズして保存します。大きなアプリケーションでは、MemecachedやAPCのような、メモリに保存するキャッシュを使用することをおすすめします。The cache configuration file also contains various other options, which are documented within the file, so make sure to read over these options. By default, Laravel is configured to use the file cache driver, which stores the serialized, cached objects in the filesystem. For larger applications, it is recommended that you use an in-memory cache such as Memcached or APC. You may even configure multiple cache configurations for the same driver.
LaravelでRedisキャシュを使用する場合、predis/predisパッケージ(~1.0)をComposerでインストールしておく必要があります。Before using a Redis cache with Laravel, you will need to install the predis/predis package (~1.0) via Composer.
キャッシュの使用Cache Usage
キャッシュへアイテムを保存Storing An Item In The Cache
Cache::put('key', 'value', $minutes);
Carbonオブジェクトを有効日時として設定Using Carbon Objects To Set Expire Time
$expiresAt = Carbon::now()->addMinutes(10);
Cache::put('key', 'value', $expiresAt);
存在していなければ、キャッシュにアイテムを保存Storing An Item In The Cache If It Doesn't Exist
Cache::add('key', 'value', $minutes);
addメソッドは本当にキャッシュに追加した場合のみtrueを返します。それ以外の場合はfalseを返します。The add method will return true if the item is actually added to the cache. Otherwise, the method will return false.
キャッシュ中の存在チェックChecking For Existence In Cache
if (Cache::has('key'))
{
//
}
キャッシュからアイテム取得Retrieving An Item From The Cache
$value = Cache::get('key');
アイテムを一つ取得するかデフォルト値取得Retrieving An Item Or Returning A Default Value
$value = Cache::get('key', 'default');
$value = Cache::get('key', function() { return 'default'; });
アイテムを永久に保存Storing An Item In The Cache Permanently
Cache::forever('key', 'value');
場合により、キャッシュからアイテムを取得するとき、それが存在しない場合にはデフォルト値を保存したいこともあるでしょう。それにはCache::rememberメソッドを使用できます。Sometimes you may wish to retrieve an item from the cache, but also store a default value if the requested item doesn't exist. You may do this using the Cache::remember method:
$value = Cache::remember('users', $minutes, function()
{
return DB::table('users')->get();
});
更にrememberとforeverを一緒に使うこともできます。You may also combine the remember and forever methods:
$value = Cache::rememberForever('users', function()
{
return DB::table('users')->get();
});
キャッシュには全アイテムがシリアライズして保存されることに注目してください。ですからどんなタイプのデーターでも自由に保存できます。Note that all items stored in the cache are serialized, so you are free to store any type of data.
キャッシュからのアイテム取り出しPulling An Item From The Cache
キャッシュからあるアイテムを引き出し、その後削除したい場合は、pullメソッドを使用します。If you need to retrieve an item from the cache and then delete it, you may use the pull method:
$value = Cache::pull('key');
キャッシュからアイテム消去Removing An Item From The Cache
Cache::forget('key');
特定のキャッシュ保存域へアクセスAccess Specific Cache Stores
複数のキャッシュ保存域を使用している場合、storeメソッドによりアクセスできます。When using multiple cache stores, you may access them via the store method:
$value = Cache::store('foo')->get('key');
増減操作Increments & Decrements
database以外の全ドライバーで、incrementとdecrement操作をサポートしています。All drivers except database support the increment and decrement operations:
値の増加Incrementing A Value
Cache::increment('key');
Cache::increment('key', $amount);
値の減少Decrementing A Value
Cache::decrement('key');
Cache::decrement('key', $amount);
キャッシュタグCache Tags
**注目:**キャッシュタグは
fileとdatabaseキャッシュドライバーにはサポートされていません。さらに"forever"で保存されているキャッシュに多くのタグを使用する場合、古いレコードを自動パージするmemcachedのようなドライバーで最高のパフォーマンスが出せるでしょう。Note: Cache tags are not supported when using thefileordatabasecache drivers. Furthermore, when using multiple tags with caches that are stored "forever", performance will be best with a driver such asmemcached, which automatically purges stale records.
タグ付けしたキャッシュへアクセスAccessing A Tagged Cache
キャッシュタグにより、キャシュの中のアイテムをタグに関連付けることができるだけでなく、名前を指定しタグ付けした全キャッシュを消去することもできます。タグ付けしたキャッシュにアクセスするには、tagsメソッドを使用して下さい。Cache tags allow you to tag related items in the cache, and then flush all caches tagged with a given name. To access a tagged cache, use the tags method.
タグ付けしたキャッシュに保存するには、順序に沿って、引数にタグ名のリスト、もしくは配列を渡します。You may store a tagged cache by passing in an ordered list of tag names as arguments, or as an ordered array of tag names:
Cache::tags('people', 'authors')->put('John', $john, $minutes);
Cache::tags(['people', 'artists'])->put('Anne', $anne, $minutes);
remember、forever、rememberForeverを含め、全キャッシュ保存メソッドとタグを組み合わせて利用できます。また、incrementとdecrementのようなその他のキャッシュメソッドも同様に、タグ付けしたキャッシュのアイテムへアクセスできます。You may use any cache storage method in combination with tags, including remember, forever, and rememberForever. You may also access cached items from the tagged cache, as well as use the other cache methods such as increment and decrement.
キャッシュ中のタグ付きアイテムへアクセスAccessing Items In A Tagged Cache
タグ付けしたキャシュへアクセスするには、保存したのと同じ順番でタグのリストを渡してください。To access a tagged cache, pass the same ordered list of tags used to save it.
$anne = Cache::tags('people', 'artists')->get('Anne');
$john = Cache::tags(['people', 'authors'])->get('John');
名前、または名前のリストを指定し、タグ付けしたアイテムを全部消去できます。例えば、次のコードにより、peopleかauthors、またはその両方にタグ付けされた全キャッシュ済みアイテムが削除されます。ですから、"Anne"と"John"両アイテムはキャッシュから削除されます。You may flush all items tagged with a name or list of names. For example, this statement would remove all caches tagged with either people, authors, or both. So, both "Anne" and "John" would be removed from the cache:
Cache::tags('people', 'authors')->flush();
その一方、次のコードではauthorsタグ付けされたキャッシュだけが削除されるため、"John"は削除されますが、"Anne"は残ります。In contrast, this statement would remove only caches tagged with authors, so "John" would be removed, but not "Anne".
Cache::tags('authors')->flush();
イベントキャッシュCache Events
キャッシュの全操作ごとにコードを実行する必要があるならば、キャッシュにより発行されるイベントを購読してください。To execute code on every cache operation, you may listen for the events fired by the cache:
Event::listen('cache.hit', function($key, $value) {
//
});
Event::listen('cache.missed', function($key) {
//
});
Event::listen('cache.write', function($key, $value, $minutes) {
//
});
Event::listen('cache.delete', function($key) {
//
});
データベースキャッシュDatabase Cache
databaseキャッシュドライバーを使う場合、キャッシュアイテムを保存するテーブルを用意する必要があります。テーブル宣言を行うSchemaの一例です。When using the database cache driver, you will need to setup a table to contain the cache items. You'll find an example Schema declaration for the table below:
Schema::create('cache', function($table)
{
$table->string('key')->unique();
$table->text('value');
$table->integer('expiration');
});
MemcachedキャッシュMemcached Cache
Memcachedキャッシュを使用するためには、Memcached PECLパッケージをインストールする必要があります。Using the Memcached cache requires the Memcached PECL package[http://pecl.php.net/package/memcached] to be installed.
デフォルト設定では、Memcached::addServerに基づいたTCP/IPを使用します。The default configuration[#configuration] uses TCP/IP based on Memcached::addServer[http://php.net/manual/en/memcached.addserver.php]:
'memcached' => array(
array('host' => '127.0.0.1', 'port' => 11211, 'weight' => 100),
),
hostオプションにUNIXソケットパスを指定することも可能です。その場合、portオプションに0を指定してください。You may also set the host option to a UNIX socket path. If you do this, the port option should be set to 0:
'memcached' => array(
array('host' => '/var/run/memcached/memcached.sock', 'port' => 0, 'weight' => 100),
),
RedisキャッシュRedis Cache
Redis設定を参照してください。See Redis Configuration[/docs/redis#configuration]