Cara Membuat Command Laravel untuk Mengelola Cache Otomatis di Hosting cPanel Gratis Domain
Jika kamu menggunakan Laravel di hosting cPanel gratis domain, mengelola cache secara manual sering memakan waktu. Laravel memiliki sistem cache untuk route, config, view, dan services, namun menjalankan perintah satu per satu tidak efisien.
Pada panduan ini, kamu akan belajar cara membuat Artisan command khusus bernama system:cache untuk mengaktifkan, menonaktifkan, dan memeriksa status semua cache Laravel secara otomatis.
Fitur Utama
Mengaktifkan semua cache Laravel sekaligus.
Membersihkan cache dengan satu perintah.
Menampilkan status cache dalam bentuk tabel.
1. Membuat File Command
Buat file baru di app/Console/Commands/SystemCacheCommand.php lalu isi dengan kode berikut:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
class SystemCacheCommand extends Command
{
protected $signature = 'system:cache
{--disable : Disable (clear) all Laravel cache}
{--status : Show current cache status}';
protected $description = 'Enable, disable, or show status of Laravel caches (route, config, view, optimize)';
public function handle(): int
{
if ($this->option('status')) {
return $this->showStatus();
}
if ($this->option('disable')) {
$this->disableCache();
} else {
$this->enableCache();
}
return Command::SUCCESS;
}
protected function enableCache(): void
{
$this->info('Enabling all Laravel cache...');
$this->callSilent('route:cache');
$this->callSilent('config:cache');
$this->callSilent('view:cache');
$this->line('Optimizing class autoload...');
@exec('composer dump-autoload --optimize');
$this->info('Laravel cache enabled and optimized.');
}
protected function disableCache(): void
{
$this->info('Clearing all Laravel cache...');
$this->callSilent('route:clear');
$this->callSilent('config:clear');
$this->callSilent('view:clear');
$this->callSilent('cache:clear');
$this->callSilent('clear-compiled');
$this->purgeViewCompiled();
$this->info('All caches cleared.');
}
protected function showStatus(): int
{
$this->info('Checking cache status...');
$rows = [];
$rows[] = $this->statRow('Route cache', base_path('bootstrap/cache/routes-v7.php'));
$rows[] = $this->statRow('Config cache', base_path('bootstrap/cache/config.php'));
$rows[] = $this->statRow('Events cache', base_path('bootstrap/cache/events.php'));
$rows[] = $this->statRow('Packages cache', base_path('bootstrap/cache/packages.php'));
$rows[] = $this->statRow('Services cache', base_path('bootstrap/cache/services.php'));
[$viewCount, $viewSize, $viewMtime] = $this->statViewCompiled();
$rows[] = [
'Component' => 'View cache',
'Status' => $viewCount > 0 ? 'Generated' : 'Empty',
'Files' => $viewCount,
'Size' => $this->formatBytes($viewSize),
'Updated' => $this->formatMtime($viewMtime),
'Path' => 'storage/framework/views/*.php',
];
$this->table(
['Component', 'Status', 'Files', 'Size', 'Updated', 'Path'],
$rows
);
return Command::SUCCESS;
}
protected function statRow(string $label, string $path): array
{
if (is_file($path)) {
return [
'Component' => $label,
'Status' => 'Generated',
'Files' => 1,
'Size' => $this->formatBytes(filesize($path) ?: 0),
'Updated' => $this->formatMtime(@filemtime($path)),
'Path' => $this->shortPath($path),
];
}
return [
'Component' => $label,
'Status' => 'Missing',
'Files' => 0,
'Size' => '-',
'Updated' => '-',
'Path' => $this->shortPath($path),
];
}
protected function statViewCompiled(): array
{
$dir = storage_path('framework/views');
$pattern = $dir . DIRECTORY_SEPARATOR . '*.php';
$files = glob($pattern) ?: [];
$count = 0; $bytes = 0; $latest = null;
foreach ($files as $f) {
if (is_file($f)) {
$count++;
$bytes += filesize($f) ?: 0;
$mtime = @filemtime($f);
if ($mtime && ($latest === null || $mtime > $latest)) {
$latest = $mtime;
}
}
}
return [$count, $bytes, $latest];
}
protected function purgeViewCompiled(): void
{
foreach (glob(storage_path('framework/views/*.php')) ?: [] as $f) {
@unlink($f);
}
}
protected function formatBytes(int $bytes): string
{
if ($bytes setTimestamp($mtime)->toDateTimeString();
} catch (\Throwable $e) {
return date('Y-m-d H:i:s', $mtime);
}
}
protected function shortPath(string $path): string
{
$base = base_path();
return Str::startsWith($path, $base)
? ltrim(Str::replaceFirst($base, '', $path), DIRECTORY_SEPARATOR)
: $path;
}
}
2. Tidak Perlu Registrasi Command Secara Manual
Mulai Laravel 10 ke atas, semua command yang ada di folder app/Console/Commands otomatis terdaftar di Artisan. Jadi kamu tidak perlu menambahkannya lagi di app/Console/Kernel.php.
3. Cara Menggunakan
Mengaktifkan Cache
php artisan system:cacheMenonaktifkan Cache
php artisan system:cache --disableMelihat Status Cache
php artisan system:cache --statusKeuntungan Menggunakan Command Ini
Satu perintah untuk semua operasi cache Laravel.
Proses deployment lebih cepat dan efisien.
Monitoring cache lebih mudah.
Cocok untuk server shared atau hosting cPanel gratis domain.
FAQ Penggunaan Cache di laravel
1. Apakah command ini aman digunakan di production?
Ya, command ini hanya menjalankan perintah bawaan Laravel seperti route:cache, config:cache, dan view:cache. Tidak ada perintah berisiko yang memodifikasi data.
2. Apakah bisa digunakan di Laravel 10 dan 11?
Bisa. Script ini kompatibel mulai dari Laravel 8 hingga Laravel 11 tanpa perubahan tambahan.
3. Apakah perlu akses root server?
Tidak perlu. Cukup gunakan akses SSH biasa atau terminal di cPanel. Semua operasi berjalan di level project Laravel.
4. Bagaimana jika composer dump-autoload dibatasi di hosting?
Jika hosting kamu tidak mengizinkan menjalankan perintah composer, hapus baris @exec('composer dump-autoload --optimize');. Cache lain tetap berfungsi normal. Namun jika kamu menggunakan hosting gratis domain di antmediahost, maka fitur composer sudah tersedia dan dapat digunakan dengan baik
5. Apakah bisa digunakan dalam CI/CD pipeline?
Bisa. Tambahkan perintah php artisan system:cache pada tahap deploy agar cache otomatis dibuat setiap kali aplikasi dirilis.
Kesimpulan
Dengan command system:cache, pengelolaan cache Laravel menjadi jauh lebih praktis. Kamu bisa mengaktifkan, menonaktifkan, dan memeriksa status cache hanya dengan satu perintah. Solusi ini sangat cocok bagi developer yang menggunakan hosting cPanel gratis domain dan ingin menjaga performa website tetap maksimal.









