apcu_cas ၏အဓိပ္ပါယ်သည်အောက်ပါအတိုင်းဖြစ်သည် -
bool apcu_cas(string $key, int|float $old, int|float $new)
ဤလုပ်ဆောင်ချက်၏လုပ်ဆောင်ချက်မှာ - လက်ရှိသော့ချက်၏တန်ဖိုးသည် $ key ၏တန်ဖိုးသည် အဟောင်း နှင့်ညီမျှပါက၎င်း၏တန်ဖိုးသည် ဒေါ်လာအသစ် နှင့်ပြည့်စုံပြီး မှန်ကန်သည် ။ ဒီလိုမှမဟုတ်ရင် မှားယွင်းတဲ့ ပြန်လာတယ်။
သတိပြုသင့်သည် ( Int သို့မဟုတ် float ) ၏တန်ဖိုးများအတွက်သာအသုံးပြုရန်အတွက်သာအသုံးပြုနိုင်သည်။ သို့မဟုတ်စစ်ဆင်ရေးကျရှုံးလိမ့်မည်။
apcu_store('counter', '5');
$result = apcu_cas('counter', 5, 10);
မေးခွန်းဖော်ပြချက်:
'5' သည် apcu_cas အတွင်းရှိနံပါတ်တစ်ခုနှင့်တူနေစဉ်စစ်ဆေးမှုသည်တင်းကျပ်သည်။ String '5' သည်ကိန်း 5 နှင့်ညီမျှခြင်းမဟုတ်ပါ, ထို့ကြောင့် CAS လည်ပတ်မှုပျက်ပြားသွားလိမ့်မည်။
ဖြေရှင်းချက် -
သိုလှောင်သည့်အခါဂဏန်းအမျိုးအစားကိုအသုံးပြုရန်သေချာအောင်လုပ်ပါ။
apcu_store('counter', 5);
သို့မဟုတ်နှိုင်းယှဉ်ခြင်းမပြုမီသွန်း:
$value = apcu_fetch('counter');
if (is_numeric($value)) {
apcu_store('counter', (int)$value);
}
apcu_store('rate', 1.0);
apcu_cas('rate', 1, 2.0);
မေးခွန်းဖော်ပြချက်:
1 နှင့် 1.0 သည်သင်္ချာညီမျှသည်ရှိသော်, APCu_CAS သည် နှိုင်းယှဉ်ခြင်းနှင့် ကိုက်ညီမှု မရှိသောနှင့် float သည် ထည့်သွင်းစဉ်းစားပါလိမ့်မည်။
ဖြေရှင်းချက် -
တသမတ်တည်းကိန်းဂဏန်းအမျိုးအစားကို အသုံးပြု. မူလတန်ဖိုးအမျိုးအစားအရ parameters များကို pass လုပ်ရန်အကြံပြုသည်။
$value = apcu_fetch('rate');
if (is_float($value)) {
apcu_cas('rate', 1.0, 2.0);
}
apcu_delete('score');
$result = apcu_cas('score', 0, 1);
မေးခွန်းဖော်ပြချက်:
အကယ်. သော့မရှိသေးပါက apcu_cas သည် မှားယွင်းစွာ ပြန်ပို့လိမ့်မည်။ ၎င်းသည် developer များကိုမှားယွင်းစွာယုံကြည်စေနိုင်သည်မှာစစ်ဆင်ရေးပျက်ကွက်မှုသည်အဓိကတည်ရှိမှုမဟုတ်ဘဲကွဲပြားခြားနားသောတန်ဖိုးများကြောင့်ကွဲပြားခြားနားသောတန်ဖိုးများကြောင့်ဖြစ်သည်ဟုမှားယွင်းစွာယုံကြည်စေနိုင်သည်။
ဖြေရှင်းချက် -
apcu_cas ကို မခေါ်မီ apcu_exists သို့မဟုတ် apcu_fetch ကို သုံးပါ။
if (apcu_exists('score')) {
apcu_cas('score', 0, 1);
} else {
apcu_store('score', 1);
}
ဂဏန်းတန်ဖိုးများကိုသိမ်းပိုက်ရန်အမြဲတမ်းအသုံးပြုရန်အတွက် Int သို့မဟုတ် float ကို အသုံးပြုပါ။ ဒေတာအရေးအသားယုတ္တိဗေဒ၏အလွှာတစ်ခုမှာ encapsulated လုပ်နိုင်သည်။
function set_numeric_apcu(string $key, int|float $value): void {
apcu_store($key, $value);
}
တူညီသောအမျိုးအစားကိုသေချာစေရန်လုံခြုံသော Wrapper function ကိုဖန်တီးပါ။ ထို့နောက် apcu_cas ကို execute လုပ်ပါ။
function safe_apcu_cas(string $key, int|float $old, int|float $new): bool {
$current = apcu_fetch($key);
if (gettype($current) !== gettype($old)) {
return false;
}
return apcu_cas($key, $old, $new);
}
တစ်ပြိုင်နက်တည်းပတ် 0 န်းကျင်တွင်တန်ဖိုးများကိုအစပျိုးခြင်းလုပ်ငန်းစဉ်သည်ရှင်းလင်းပြီးရှင်းရှင်းလင်းလင်းဖြစ်သင့်သည်။ သော့ချက်တည်ရှိပြီးအောက်ပါနည်းလမ်းများဖြင့်မှန်ကန်ကြောင်းသေချာစေနိုင်သည်။
$key = 'counter';
if (!apcu_exists($key)) {
apcu_add($key, 0);
}
APCU_CAS ကို လုံခြုံစွာအလိုအလျောက်တိုးပွားစေရန်မည်သို့အသုံးပြုရမည်ကိုပြသည့်အပြည့်အဝဥပမာတစ်ခုမှာ -
$key = 'visit_counter';
do {
$old = apcu_fetch($key);
if ($old === false) {
if (apcu_add($key, 1)) {
break;
} else {
continue;
}
}
$new = $old + 1;
} while (!apcu_cas($key, $old, $new));
$url = 'https://gitbox.net/stats';
echo "လက်ရှိလည်ပတ်မှုများ update လုပ်ပါပြီ。အသေးစိတ်အတွက် ကျေးဇူးပြု. ကြည့်ပါ: <a href=\"$url\">$url</a>";