Stick to Redis instead of APCu

This commit is contained in:
Pinga 2025-02-12 15:22:42 +02:00
parent 73e19087ae
commit fb2ef70b71
6 changed files with 87 additions and 94 deletions

View file

@ -608,14 +608,15 @@ function updatePermittedIPs($pool, $permittedIPsTable) {
}
function getDomainPrice($pdo, $domain_name, $tld_id, $date_add = 12, $command = 'create', $registrar_id = null, $currency = 'USD') {
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$cacheKey = "domain_price_{$domain_name}_{$tld_id}_{$date_add}_{$command}_{$registrar_id}_{$currency}";
// Try fetching from cache
if (function_exists('apcu_fetch')) {
$cached = apcu_fetch($cacheKey);
if ($cached !== false) {
return $cached;
}
$cached = $redis->get($cacheKey);
if ($cached !== false) {
return json_decode($cached, true); // Redis stores as string, so decode
}
$exchangeRates = getExchangeRates();
@ -623,7 +624,7 @@ function getDomainPrice($pdo, $domain_name, $tld_id, $date_add = 12, $command =
$exchangeRate = $exchangeRates['rates'][$currency] ?? 1.0;
// Check for premium pricing
$premiumPrice = apcu_fetch("premium_price_{$domain_name}_{$tld_id}") ?: fetchSingleValue(
$premiumPrice = $redis->get("premium_price_{$domain_name}_{$tld_id}") ?: fetchSingleValue(
$pdo,
'SELECT c.category_price
FROM premium_domain_pricing p
@ -636,13 +637,13 @@ function getDomainPrice($pdo, $domain_name, $tld_id, $date_add = 12, $command =
$money = convertMoney(new Money((int) ($premiumPrice * 100), new Currency($baseCurrency)), $exchangeRate, $currency);
$result = ['type' => 'premium', 'price' => formatMoney($money)];
apcu_store($cacheKey, $result, 1800);
$redis->setex($cacheKey, 1800, json_encode($result));
return $result;
}
// Check for active promotions
$currentDate = date('Y-m-d');
$promo = apcu_fetch("promo_{$tld_id}") ?: fetchSingleRow(
$promo = json_decode($redis->get("promo_{$tld_id}"), true) ?: fetchSingleRow(
$pdo,
"SELECT discount_percentage, discount_amount
FROM promotion_pricing
@ -655,12 +656,12 @@ function getDomainPrice($pdo, $domain_name, $tld_id, $date_add = 12, $command =
);
if ($promo) {
apcu_store("promo_{$tld_id}", $promo, 3600);
$redis->setex("promo_{$tld_id}", 3600, json_encode($promo));
}
// Get regular price from DB
$priceColumn = "m" . (int) $date_add;
$regularPrice = apcu_fetch("regular_price_{$tld_id}_{$command}_{$registrar_id}") ?: fetchSingleValue(
$regularPrice = json_decode($redis->get("regular_price_{$tld_id}_{$command}_{$registrar_id}"), true) ?: fetchSingleValue(
$pdo,
"SELECT $priceColumn
FROM domain_price
@ -671,7 +672,7 @@ function getDomainPrice($pdo, $domain_name, $tld_id, $date_add = 12, $command =
);
if (!is_null($regularPrice) && $regularPrice !== false) {
apcu_store("regular_price_{$tld_id}_{$command}_{$registrar_id}", $regularPrice, 1800);
$redis->setex("regular_price_{$tld_id}_{$command}_{$registrar_id}", 1800, json_encode($regularPrice));
$finalPrice = $regularPrice * 100; // Convert DB float to cents
if ($promo) {
@ -689,7 +690,7 @@ function getDomainPrice($pdo, $domain_name, $tld_id, $date_add = 12, $command =
$money = convertMoney(new Money($finalPrice, new Currency($baseCurrency)), $exchangeRate, $currency);
$result = ['type' => $type, 'price' => formatMoney($money)];
apcu_store($cacheKey, $result, 1800);
$redis->setex($cacheKey, 1800, json_encode($result));
return $result;
}
@ -697,14 +698,15 @@ function getDomainPrice($pdo, $domain_name, $tld_id, $date_add = 12, $command =
}
function getDomainRestorePrice($pdo, $tld_id, $registrar_id = null, $currency = 'USD') {
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$cacheKey = "domain_restore_price_{$tld_id}_{$registrar_id}_{$currency}";
// Try fetching from cache
if (function_exists('apcu_fetch')) {
$cached = apcu_fetch($cacheKey);
if ($cached !== false) {
return $cached;
}
$cached = $redis->get($cacheKey);
if ($cached !== false) {
return json_decode($cached, true);
}
// Fetch exchange rates
@ -734,7 +736,7 @@ function getDomainRestorePrice($pdo, $tld_id, $registrar_id = null, $currency =
// Format and cache the result
$formattedPrice = formatMoney($money);
apcu_store($cacheKey, $formattedPrice, 1800); // Cache for 30 minutes
$redis->setex($cacheKey, 1800, json_encode($formattedPrice));
return $formattedPrice;
}
@ -743,13 +745,14 @@ function getDomainRestorePrice($pdo, $tld_id, $registrar_id = null, $currency =
* Load exchange rates from JSON file with APCu caching.
*/
function getExchangeRates() {
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$cacheKey = 'exchange_rates';
if (function_exists('apcu_fetch')) {
$cached = apcu_fetch($cacheKey);
if ($cached !== false) {
return $cached;
}
$cached = $redis->get($cacheKey);
if ($cached !== false) {
return json_decode($cached, true);
}
$filePath = "/var/www/cp/resources/exchange_rates.json";
@ -762,9 +765,7 @@ function getExchangeRates() {
];
if (!file_exists($filePath) || !is_readable($filePath)) {
if (function_exists('apcu_store')) {
apcu_store($cacheKey, $defaultRates, 3600);
}
$redis->setex($cacheKey, 3600, json_encode($defaultRates)); // Cache for 1 hour
return $defaultRates;
}
@ -772,9 +773,7 @@ function getExchangeRates() {
$data = json_decode($json, true);
if (!isset($data['base_currency'], $data['rates']) || !is_array($data['rates'])) {
if (function_exists('apcu_store')) {
apcu_store($cacheKey, $defaultRates, 3600);
}
$redis->setex($cacheKey, 3600, json_encode($defaultRates)); // Cache for 1 hour
return $defaultRates;
}
@ -790,9 +789,7 @@ function getExchangeRates() {
}
}
if (function_exists('apcu_store')) {
apcu_store($cacheKey, $data, 3600);
}
$redis->setex($cacheKey, 3600, json_encode($data)); // Cache for 1 hour
return $data;
}