Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Preventing an image from caching

by belize (Deacon)
on Nov 28, 2000 at 03:29 UTC ( [id://43567]=perlquestion: print w/replies, xml ) Need Help??

belize has asked for the wisdom of the Perl Monks concerning the following question:

I now have the successful code to generate an image on the fly (see previous post at Mon Nov 27 2000 at 09:52). You can see it at http://www.belizeexplorer.com, (but only the search function is working - select "Mayan Sites" as the database is still only for testing).

The problem now is that the generated image is cached, so that to see an updated image, you have to REFRESH the screen.

Is there a way to prevent caching? or do I have to generate a random number to name each image generated on the fly?

Replies are listed 'Best First'.
Re: Preventing an image from caching
by merlyn (Sage) on Nov 28, 2000 at 03:36 UTC
    The RFC'ed way to stop an image from caching is to "ship it expired". Set the Last-Modified header to now, and expires also to now, as a proper HTTP date, of course.

    -- Randal L. Schwartz, Perl hacker

      Thanks Randal. What is the proper syntax for the above, I can't seem to find it. Would I generate the date:time within the CGI and then use a variable to place the current date and time into the header? Is there a specific syntax that the date has to be in?
        With CGI.pm, you can use the -expires switch to the header routine. Apparently, CGI::Util also has an expires routine that you can use to calculate the proper format. If you don't like digging around in the innards of CGI.pm, pull up HTTP::Date, which formats things according to the RFC. Something like this:
        use HTTP::Date qw(time2str); my $now = time2str(time); print "Expires: $now\nLast-modified: $now\n"; # add other headers here

        -- Randal L. Schwartz, Perl hacker

Re: Preventing an image from caching
by cleen (Pilgrim) on Nov 28, 2000 at 03:59 UTC
    Well if your talking about caching with proxy servers (IE Squid), by default squid and such cache's wont cache dynamic content, ie *.cgi? or *.pl? *.shtml ,or set the last updated to the current time in the headers.
Re: Preventing an image from caching
by extremely (Priest) on Nov 28, 2000 at 04:51 UTC
    Have you saved the file to disk? If so, make sure your script unlink()s the file before writing a new one. That sometimes helps me out. I ask because your last post asked how to save an image to disk...

    --
    $you = new YOU;
    honk() if $you->love(perl)

Re: Preventing an image from caching
by bastard (Hermit) on Nov 28, 2000 at 20:19 UTC
    If you are generating the page, append a random number to the call for the image.
    <IMG SRC="/cgi-bin/image.pl?rand=4531269823">
    Each time the page draws the string will be different, eliminating the cached image problem because to the browser it is not the same image.
      Thanks for the post, but I am generating the image, saving it to a file, then calling the file with the <IMG...> tag. I can't seem to get the image to go directly to the page because all I get is ASCII text instead of hte binary.
        Call the file with this script.
        #!/bin/perl $image = `cat imagenameandpath`; print "Content-type: image/png\n\n"; print $image;
        It's the simplest way i know.

        BTW- image/png is for png file format, substitute gif, jpeg, for others.

Re: Preventing an image from caching
by lemming (Priest) on Nov 29, 2000 at 01:51 UTC
    This website talks about the cache fairly clearly and shows various details on controlling the cache for different objects.
    Cache info at netapp.com
Re: Preventing an image from caching
by belize (Deacon) on Nov 28, 2000 at 17:41 UTC
    I need to make another condition here. I do not want to stop the other images on the page to not cache as they include navigation bars and other images that appear on every page. If the entire page has to be reloaded everytime, then the whole program slows down by a factor of 50. I only need the image that is generated by GD on the fly to not cache.

    All the solutions so far I believe are applied to the entire page (all images) and not only to the generated image.

    I hope I am clear and not missing something easy.

    Tony

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://43567]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-19 22:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found