Where and how to store exchange rates?

I wrote a script that receives current Paypals rates compared to the dollar every hour (the currency at which my products are by default).

The user can set his currency in his settings and store in the user table.

My initial idea was to store all currency rates in a database, and then when the user registers the currency code and exchange rate in his session. Then, around each price, I have a function that multiplies the price by the user's speed and adds a currency code to the end.

My only concern is that the session variable may exist at some point and could potentially make the price completely wrong.

Instead of storing bids in a session, I just have to save my currency code and save the bids in the memory table or in the file system for quick access and access the price conversion function? Thus, prices are as modern as rates.

How is this usually achieved?

+3
source share
4 answers

Could you instead show the prices in the currency in which they were indicated, and show the approximate price in the chosen currency, with the proviso that the actual rate may vary depending on the time of their design?

+7
source

, PayPal. , . , . , , . ( )

$currency = 'usd';
if (!$cache->has("exRate-$currency")) {
    $exRate = ForEx::find($currency);
    $cache->save("exRate-$currency");
} else {
    $exRate = $cache->get("exRate-$currency");
}
CurrencyConverter::setRate($currency, $exRate);
CurrencyConverter::convert(100, 'eur', 'usd');

APC memcached.

+2

- php.ini session.gc_maxlifetime. 1440 (24 ), .

( ) ini_set:

ini_set('session.gc_maxlifetime', 60); // set to 1 hour

Edit

, , , session.gc_probability session.gc_divisor. http://www.php.net/manual/en/session.configuration.php

0

.

Is it possible to use $ _SERVER as a global object in general, similar to an ASP Application object? If not, is there a PHP application object?

If so, you can save the exchange rate to $ _SERVER and then update it if necessary (for example, when Paypal updates the exchange rate)

0
source

Source: https://habr.com/ru/post/1728464/


All Articles