views:

69

answers:

4

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?

+1  A: 

Store the results in a database, call the scripts from a cron job as often as you want it updated .. daily, hourly, whatever makes you happy.

Erik
A: 

This can be one advantage of a good templating engine like Smarty. Smarty allows you to effetively cache parts of the page for whatever interval you like and it'll take care of it for you.

Other templating engines will probably have similar and you can of course do this yourself.

But first you should ask: is this actually a problem? Time how long it takes to render. I would actually be surprised if this is a real problem but it depends on where it's getting that information and how the load is implemented.

Alternatively if they come from something unreliable (eg a Web service) you may want to load them from an external script and store them in an in-memory cache (eg memcached) or simply in a database.

cletus
As you can see from the question, I am a php beginner, so I doubt about implementing "Smarty" or similar things.
serhio
+3  A: 

The keyword you are looking for is Caching.

In your case, the idea is to fetch the Exchange Rates only as often as they are updated. If, for instance, your script is fetching the Exchange Rates from the ECB, then the rates are changed on a daily basis (around 1400 CET). There is no need to fetch them more than once a day.

The simplest custom implementation I can think of to build a cache is to store the result from the Forex call and serialize the result to file. Then, when a second call to the service is made, check the file's last modified time. If it is older than, e.g. 24 hours, dispatch the call to the external site and replace the file. If not, unserialize the contents instead of calling the external site.

Since Caching is a common concept, there is a number of implementations to pick from, like APC, memcached and abstraction layers like Zend_Cache, PEAR's Cache or CacheLite. Smarty could cache any templates using your Forex service too, but personally I feel Smarty's time is over.

Also see this related question:

Gordon
+1  A: 

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.

Álvaro G. Vicario