Cache for icon item.

Added by Aeness almost 5 years ago


I am using JQuery and when I change/recharge the DOM HTML which display Icon Item, all pictures are reloaded.

For me, the reason is when the navigator call item_icon.php?sheetid=$sheetid&c=$c&q=$q&s=$s&sap=$sap the header does not have cache informations.

So, I tried to use my own server with a PHP page which downloads the content and changes the http header like this (here the pictures are only cached for 2 minutes):

echo ryzom_item_icon($sheetid, $c, $q, $s, $sap, $destroyed);

header('Content-type: image/png');

$mtime = gmdate('D, d M Y H:i:s', time() );

header("Cache-Control: max-age=120, must-revalidate");
header("Expires: " . $mtime . " GMT");
header("Last-Modified: " . $gmt_mtime . " GMT");
header("Pragma: cache");

But this is a waste of time and resources and for doing this I need to use ryzom_item_icon (client_functions_item_icon.php) that uses file_get_contents.
And file_get_contents needs the allow_url_fopen php option to be activated and I do not think my host will allow me to change this option.

My suggestion is, change the HTTP header of item_icon.php (could just be an option).
That could be better for your server and for the client applications.

Maybe you already have this option, because I saw the variable $use_cache in the API.


Replies (5)

RE: Cache for icon item. - Added by vl almost 5 years ago


For the item icon, the server cache is forever since the icon for a specific "c q s sap" will never change.

We'll add your code to setup the header cache and tell you when it's done so you'll be able to tell us if it's better :)

Thanks for the test.

RE: Cache for icon item. - Added by Aeness almost 5 years ago

I do not know about the quality of my code. I test it only with FF and I know each navigator use the headers differently. For example, you can see here the differences between FF and IE

I will do a little more advanced research and give you you a shout.

RE: Cache for icon item. - Added by Aeness almost 5 years ago


I have tested with IE7, FF3.0 and Chrome. The following code seems to work :

if (    isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) 
    && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= filemtime( $_SERVER['SCRIPT_FILENAME'] )
) {
    Header("HTTP/1.0 304 Not Modified");
else {
    // build the pictures
    echo ryzom_item_icon($sheetid, $c, $q, $s, $sap, $destroyed);

    // build the header
    $mtime = gmdate('D, d M Y H:i:s \G\M\T', filemtime( $_SERVER['SCRIPT_FILENAME'] ));
    $mtime2 = gmdate('D, d M Y H:i:s \G\M\T', time() + 3600);

    header('Content-type: image/png');
    header("Cache-Control: max-age=3600");
    header("Expires: ". $mtime2);
    header("Last-Modified: " . $mtime );
    header("Pragma: cache");
With FF :
  1. when you change/recharge the DOM HTML, FF uses its owns cache as long as it does not reach the cache duration given by the server,
  2. when you reload the page (F5), FF asks if the page has changed,
  3. when you come back to the page, FF uses its cache or asks, it depends on the cache duration given by the server.

With other browsers, the behaviour seems similar.

So, the browser have less things to ask and to do, and the server as well.

In this example the cache duration is one hour, but maybe you can put more.


RE: Cache for icon item. - Added by vl almost 5 years ago

Thanks for the code.

I adapted it and it's live.

The strange things is that even with that, when I press F5, it still generate a request so it takes 100ms+ to have the 304 answer. I don't understand why firefox 3.5 doesn't use the version it has locally.

I setup 10 hours for the cache timeout and use the real date of the icon file and not the date of the script.

RE: Cache for icon item. - Added by Aeness almost 5 years ago

After a few tries this seems to work.

When you press F5, firefox generates a conditional request but anyway that wast of resources, I agree and I do not know why.