0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Laravel】一括キャッシュクリアコマンド

Posted at

はじめに

  • 一括で実行できるコマンドについて調べたのでまとめてみました。

前提

  • Laravel10

一括でキャッシュクリア

php artisan optimize:clear
  • Laravelによるキャッシュクリアが全て実行されていることが確認できる

スクリーンショット 2024-07-21 15.42.38.png

-ファイル中確認すると下記のようになっている。

vendor/laravel/framework/src/Illuminate/Foundation/Console/OptimizeClearCommand.php

<?php

namespace Illuminate\Foundation\Console;

use Illuminate\Console\Command;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'optimize:clear')]
class OptimizeClearCommand extends Command
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'optimize:clear';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Remove the cached bootstrap files';

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle()
    {
        $this->components->info('Clearing cached bootstrap files.');

        collect([
            'events' => fn () => $this->callSilent('event:clear') == 0,
            'views' => fn () => $this->callSilent('view:clear') == 0,
            'cache' => fn () => $this->callSilent('cache:clear') == 0,
            'route' => fn () => $this->callSilent('route:clear') == 0,
            'config' => fn () => $this->callSilent('config:clear') == 0,
            'compiled' => fn () => $this->callSilent('clear-compiled') == 0,
        ])->each(fn ($task, $description) => $this->components->task($description, $task));

        $this->newLine();
    }
}

各コマンドを調べてみる

event

  • イベントとリスナーの紐付けキャッシュを削除している
php artisan event:clear

views

  • resources/views/* に存在するviewテンプレートのキャッシュを削除している
php artisan view:clear

cache

  • cacheクラスを使用したキャッシングを削除している。参考記事によるとCache::flush();と同じ処理が走るらしい。
php artisan cache:clear
  • 公式ドキュメントに記載があったけど、keyに関係なく全て消されるので注意が必要そう

キャッシュをフラッシュすると、設定されたキャッシュの「プレフィックス」は考慮されず、キャッシュからすべてのエントリが削除されます。他のアプリケーションによって共有されているキャッシュをクリアする場合は、この点を慎重に考慮してください。

route

  • routes/* に存在するルーティングに関する記述のキャッシングを削除している
php artisan route:clear

config

  • config/* に存在する複数のファイルを1つのファイルにキャッシングしているものを削除している
php artisan config:clear

clear-compiled

  • Laravelアプリケーションのコンパイル済みファイルとキャッシュを削除している。
php artisan clear-compiled

まとめ

  • 一括でキャッシュクリアできるのは便利な一方で消す必要のないキャッシュまで削除しているので、便利だけど考えて使った方が良さそう。調べるきっかけになったので勉強になった。

参考

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?