V
V
Vasyl Fomin2016-12-23 12:09:30
Laravel
Vasyl Fomin, 2016-12-23 12:09:30

Why doesn't Intervention Image Cache cache images?

For a site on Laravel 5.3, I installed the Intervention Image Cache plugin for caching images, published configs, set paths.
Made a route for pictures. The problem is that every time I access the image through this route, as I understand it again, it is cut off and cached, although the image should already be in the cache, and accordingly, all images on the site are loaded for a long time, although their size is 30-100 KB.
The solution with setting the response , for caching on the browser side does not change anything ...
Here is my code where I cache images:

$img = Image::cache(function($image) use($path) {
        return $image->make($path)->resize(420, 360);
}, 43200, false);

And the response code for the route:
$mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $img);
        return new IlluminateResponse($img, 200, [
            'Content-Type' => $mime,
            'Cache-Control' => 'max-age=' . 43200) . ', public',
            'Etag' => md5($img)
        ]);

Theoretically, everything should work, the documentation says ( third paragraph ) that each call to "Image::cache" checks whether the image is in the cache, if so, it takes it from there, if not, it caches it.
And here are my timings from the browser:
ef85968ba6cd487ebcd95c973fb33050.png9060e4132c0b4c809ec4ac1c6ab70fa1.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2016-12-23
@Nc_Soft

Here I look at such caching attempts and it just causes me bewilderment. Are you seriously going to pull php for every request for a picture?
If you want to resize images automatically, then cache through nginx, and through this useless cache function

fastcgi_cache_path  /home/www/nginx levels=1:2 keys_zone=CACHE:256m max_size=100m inactive=100d; 

    server {
        server_name domain.ltd;
        listen 80;

        location ~ /image/resize/([0-9]+) {
            fastcgi_cache CACHE;
            fastcgi_cache_key "domain.ltd$request_uri";
            fastcgi_cache_use_stale updating error timeout http_500;
            fastcgi_cache_valid 200 302 304 30d;
            fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
            add_header X-Cache $upstream_cache_status;

            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php;
           fastcgi_param SCRIPT_FILENAME /home/www/domain.ltd/public/index.php; 
            include fastcgi_params;

Well, the script itself, which resizes (it works out once, then nginx gives the image from the cache without pulling php)
$img = Image::make($path)->resize(420, 360);
return $img->response('jpg');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question