开发者

php caching problem

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开发者_JS百科 = 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?


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.


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.


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">

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜