https://laravel.com/docs/8.x/cache
Cache - Laravel - The PHP Framework For Web Artisans
Cache Introduction Some of the data retrieval or processing tasks performed by your application could be CPU intensive or take several seconds to complete. When this is the case, it is common to cache the retrieved data for a time so it can be retrieved qu
laravel.com
위 참고,
config / database.php 수정
'redis' => [
'cache' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_CACHE_DB', 1),
],
],
사용 방법
$lists = Cache::remember('list_cache', 60, function(){ //60분 캐싱
$listsDb = User::with('category')
->sort()
->take(10)
->get();
return $listsDb;
});
위 코드 설명
Redis 에서 list_cache 라는 키를 조회 후, 있으면 Redis에서 내용을 가져오고, 없으면 function 안에있는 내용을 실행한다.
Redis의 내용은 60분 후 갱신된다.
클로저 응답(return) 은 Collection 형태로 해야 한다.
Builder 타입으로 응답할 경우 Closure 에러 난다.
클로저 내부 변수 사용의 경우
$lists = Cache::remember('list_cache', 60, function() use ($count) { //60분 캐싱
$listsDb = User::with('category')
->sort()
->take($count)
->get();
return $listsDb;
});
위와 같이 use 로 변수 사용
반응형
'Laravel' 카테고리의 다른 글
[Laravel] Collection 존재 확인 (0) | 2021.08.27 |
---|---|
[Laravel] Query 실행 분석하기 (0) | 2021.08.09 |
[Laravel] Collection 을 Log로 찍을때 예쁘게 보는 법 (0) | 2021.05.26 |
[Laravel] 라라벨 log 파일에 로그 출력하기 (0) | 2021.02.19 |
[Laravel] Timestamp 값 계산 / 비교 (0) | 2020.12.23 |