Files
citrus-cms/app/Functions/setting.php
T
2026-04-28 21:14:25 +03:00

56 lines
1.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php function setting($key, $strip_tags=false, $default="") {
// Cache kaldırıldı, doğrudan veritabanından oku
$setting = db("settings")->where("title", $key)->orderBy("id", "DESC")->first();
if ($setting) {
if ($strip_tags) {
$setting->html = strip_tags($setting->html);
}
return $setting->html;
} else {
// Eğer ayar yoksa, default ile oluştur
ekle2([
'title' => $key,
'html' => $default
], "settings");
return $default;
}
}
function setting_put($key, $data)
{
// Use transaction with retry logic to prevent deadlocks
$maxRetries = 3;
$attempt = 0;
while ($attempt < $maxRetries) {
try {
DB::transaction(function() use ($key, $data) {
db("settings")->updateOrInsert([
'title' => $key
],
[
'title' => $key,
'html' => $data
]);
});
// Cache the value for faster reads
Cache::put("setting_{$key}", $data, 3600); // 1 hour cache
return true;
} catch (\Illuminate\Database\QueryException $e) {
// If deadlock detected, retry
if ($e->getCode() == '40001' || strpos($e->getMessage(), 'Deadlock') !== false || strpos($e->getMessage(), 'Lock wait timeout') !== false) {
$attempt++;
if ($attempt >= $maxRetries) {
throw $e; // Rethrow if max retries reached
}
usleep(100000 * $attempt); // Wait 100ms * attempt number before retry
} else {
throw $e; // Rethrow other exceptions immediately
}
}
}
}
?>