2

I have a class for caching some images

<?php
class Vp_CacheImage
{
    public function cache($cacheTime, $place) // $place = id of image (between 1 and 100)
    {
        $send_body = true; 
        $etag = '"' . md5(date('d.m.Y', time()) . $place) . '"';
        header("ETag: " . $etag );
        header("Last-modified: " . gmdate("D, d M Y H:i:s", $cacheTime) . " GMT");

        $inm = explode(',', getenv("HTTP_IF_NONE_MATCH"));
        foreach ($inm as $i)
        {
            if (trim($i) == $etag || trim($i) == $cacheTime)
            {
                header ("HTTP/1.0 304 Not Modified");
                $send_body = false;
            }
        }

        if(getenv("HTTP_IF_MODIFIED_SINCE") == gmdate("D, d M Y H:i:s", $cacheTime). " GMT")
        {
            header ("HTTP/1.0 304 Not Modified");
            $send_body = false;
        }

        header("Expires: " . gmdate("D, d M Y H:i:s", time() + Config::$config['cache']['images']['topvideo']) . " GMT");
        header("Cache-Control: max-age=" . Config::$config['cache']['images']['topvideo'] . ", must-revalidate");
        if ($send_body) return true; else return false;
    }
}

It works. But I have to make a picture reloaded sometimes, and not taken from cache. How to do it?

3 Answers 3

2

Then you have to remove it from your cache first, or.. add temp. a random token, like: ?x=234 behind it and reload it.

You can also set expire to the moment right before you reload it, but that's tricky.

Sign up to request clarification or add additional context in comments.

1 Comment

You can also set expire to the moment right before you reload it, but that's tricky. might be not possible while you probably don't know when you need to reload it.
0

Can you simply append a version to the image URL? Example: myimage.jpg?v=10

Doing so will cause the browser to request a fresh copy of the image rather than use the cached one.

Comments

0

The easiest way of forcing the browser to fetch the image again is to append a query string to the end of the filename.

e.g. <img src="/assets/images/abc.png?v2">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.