Forums » Ryzom API suggestions »
Cache for icon item.
Added by Aeness almost 6 years ago
Hello,
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.
Aeness
Replies (5)
RE: Cache for icon item. - Added by vl almost 6 years ago
Hello,
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 6 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 6 years ago
Hello,
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 :
- 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,
- when you reload the page (F5), FF asks if the page has changed,
- 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.
Aeness
RE: Cache for icon item. - Added by vl almost 6 years ago
Thanks for the code.
I adapted it and it's live.
http://atys.ryzom.com/api/item_icon.php?sheetid=icmm2ms_3.sitem&c=-1&q=100&s=1&sap=-1
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 6 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.
Thanks
(1-5/5)