2 years ago

#10878

test-img

user17027702

PHP caching image https request

I have a very high I/O load on the server which causes 500 error many times per day while the CPU/RAM usage is basically non exsitent:

enter image description here

the main work of the server is serving images and videos. I'll focus the question on images and leave videos for another question.

This is the code for serving images:

$file = $_GET['n'];
$path = $root . '/memes/' . getDir($file) . '/' . $file;

header('Content-Type: image/jpeg');
header('Content-Disposition: inline; filename="' . $file . '"');
header('Content-Length: ' . filesize($path));
header('Cache-control: max-age='.(60*60*24*365));
readfile($path);

I guess header('Cache-control: max-age='.(60*60*24*365)); is for browser..

How can I cache the image into RAM and then serve it from there instead of putting the load on the SSD hunderts of times for the same image with PHP? I also need to set expire time, so if a specific image doesn't get requested for like 5 minutes it should be removed from the RAM / cache.

I want to make use of the CPU and RAM instead of letting them being almost useless :D

php

header

http-caching

0 Answers

Your Answer

Accepted video resources