နားလည်ထားရမယ့် အချက်
String က text၊ number သို့ binary bytes ကိုသိမ်းနိုင်သော အခြေခံ data type ပါ။ `INCR` က concurrent clients များကြား atomic counter ပေးပြီး `SET ... NX/XX EX/PX` က conditional write နဲ့ expiry ကို command တစ်ခုတည်းမှာလုပ်နိုင်ပါတယ်။ Key ကို `SET` ပြန်လုပ်ရင် TTL ပျောက်နိုင်လို့ `KEEPTTL` သို့ explicit expiry policy လိုပါတယ်။
လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်
Page view counter ကို `INCR`၊ email verification code ကို `SET ... EX 600 NX`၊ catalog fragment ကို JSON string + TTL အဖြစ်သိမ်းမယ်။ `TTL` return values `-1` (expiry မရှိ) နဲ့ `-2` (key မရှိ) ကို application မှခွဲကိုင်ရပါတယ်။
အတူတူ စမ်းရေးကြည့်မယ်
INCR metrics:tutorial:42:views
SET verify:email:user:42 834921 EX 600 NX
GET verify:email:user:42
TTL verify:email:user:42
SET tutorial:42:card '{"title":"Redis"}' EX 300
GET tutorial:42:cardAtomic counter၊ expiring verification code နှင့် five-minute cache key ရမည်။၅ မိနစ် စမ်းကြည့်
Download counter၊ one-time idempotency key နဲ့ 30-second API response cache သုံးမျိုးကို command များဖြင့်ရေးပါ။
သတိလေးတစ်ချက်
Counter `INCR` ပြီးမှ `EXPIRE` သီးခြားလုပ်ရာတွင် process crash လျှင် expiry မပါတဲ့ key ကျန်နိုင်ပါတယ်။ Transaction သို့ script ဖြင့် atomic လုပ်ပါ။
Redis — Strings — Redis