본문으로 바로가기

라라벨 캐시 사용하기

category [ Web 관련 ]/Laravel 12.5.0 2026. 2. 13. 11:22

 

스팸단어 체크등에 캐시 사용

use Illuminate\Support\Facades\Cache;


$spam_words = Cache::remember('spam_words', 600, function () {
    return DB::table('TB_Spam')
        ->whereNotNull('sp_word')
        ->where('sp_word','<>','')
        ->pluck('sp_word');
});

🔎 1️⃣ 'spam_words' 는 캐시 키 이름

메모리에 저장할 이름.

🔎 2️⃣ 600은 초 단위

600초 = 10분

👉 10분 동안은 DB 안 조회함

🔎 3️⃣ 동작 방식

📌 첫 요청

  • 'spam_words' 캐시에 없음
  • DB 조회 실행
  • 결과 저장
  • 결과 반환

📌 10분 안의 다음 요청들

  • 캐시에 있음
  • DB 조회 안 함
  • 저장된 데이터 바로 반환

 

 

.env 파일 에서 캐시 드라이버 file로 변경

CACHE_DRIVER=file

 

변경후 초기화

php artisan config:clear
php artisan config:cache
php artisan cache:clear

 

 

만약 권한 문제 발생하면

sudo chown -R apache:apache storage
sudo chown -R apache:apache bootstrap/cache


sudo chmod -R 775 storage
sudo chmod -R 775 bootstrap/cache

 

 

ec2-user로 실행이 안될수 있음.

apache 그룹에 ec2-user 계정이 없다면 추가

sudo usermod -aG apache ec2-user

실행 후

SSH 재접속 필요

 

 

이제 캐시는 아래 경로에 저장됨

storage/framework/cache/data