pathurs
pathurs

Reputation: 643

Browser temporary cache

I am creating a dynamic image dependent on a lot of information about the logged in user, but with so many users online, the image system is using a lot of the resources my website hosts have allocated to me.

Is there a way to tell the browser to temporarily cache an image? Similar to temporary cookies?

The image contains numbers such as post count and other frequently incrementing numbers that can cause the browser's automatically cached images to re-download the image potentially every page load. I wouldn't mind a little inaccuracy to save a lot of processing for my server

Upvotes: 0

Views: 76

Answers (2)

Ne Ma
Ne Ma

Reputation: 1709

[Edit] Browsers already do cache content(unless specifically told not to do so), if it detects a change in last modified then it reloads it.

I am taking a guess that your image being generated is either a) being returned to the browser by a specific php script() or b) is being stored temporarily but is being updated and so the browser retrieves the updated version. [/Edit]

If you are outputting an image based on dynamically generated content you can write this temporarily to a directory and then based on a predefined time period pull the image if its valid.

if(file_exists($file) && filemtime($file) <= time()-60*20) {
    // Regenerate image
} else {
    // Load image from cache
}

The example above will not regenerate the image for 20 minutes.

Upvotes: 1

Andrej  Bestuzhev
Andrej Bestuzhev

Reputation: 674

Try to look here http://www.webscalingblog.com/performance/caching-http-headers-cache-control-max-age.html

In other way, you can set up cache-control header in PHP, and display images using script.

Upvotes: 0

Related Questions