I have a site in multiple languages. I have a method that returns me the today currencies in a array. I display that currencies in a table then.
// --- en/index.php
<?php
include_once "../exchangeRates.php";
$currencies = ReadExchangeRates();
// --- fr/index.php
<?php
include_once "../exchangeRates.php";
$currencies = ReadExchangeRates();
...
// somewhere in the page
<td><?php echo $currencies["eur"]["today"]; ?></td>
So, every time I load, en/ or fr/ or other language, I request the exchange rates from a external site.
Can I optimize this behavior (reading once per day or session)?
maybe to store a global variable and check the update date?
All variables die with the script, global or local, they can’t be used for cache purposes.
You have several solutions depending on your resources:
You can pick the exchange rates, serialize them as a string and store it into a file. When you you need to print them, read the file modification time; if it’s older than X, fetch fresh data from the external server, otherwise unserialize your current data and display it.
And, of course, you can simply store raw data in a proper SQL database and update it in regular basis.