Thuta Learning
ရှာဖွေရန်
Redis
BasicData & Databasesbeginner

Strings၊ Counters၊ EXPIRE နှင့် TTL

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Strings၊ Counters၊ EXPIRE နှင့် TTL concept ကို နားလည်ရှင်းပြနိုင်ရန်
  • နမူနာ Redis command/code ကို ကိုယ်တိုင် run ပြီး output စစ်နိုင်ရန်
  • Tutorial Platform project နှင့် production scenario တွင် မှန်ကန်စွာအသုံးချနိုင်ရန်

နားလည်ထားရမယ့် အချက်

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 မှခွဲကိုင်ရပါတယ်။

အတူတူ စမ်းရေးကြည့်မယ်

shell
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:card
You should see
Atomic 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 — StringsRedis

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Counter `INCR` ပြီးမှ `EXPIRE` သီးခြားလုပ်ရာတွင် process crash လျှင် expiry မပါတဲ့ key ကျန်နိုင်ပါတယ်။ Transaction သို့ script ဖြင့် atomic လုပ်ပါ။
  • နမူနာ command ကို production Redis ပေါ် တိုက်ရိုက်မစမ်းဘဲ local/test instance နဲ့ recoverable data ပေါ်တွင် အရင်အတည်ပြုပါ။

လေ့ကျင့်ခန်း

Download counter၊ one-time idempotency key နဲ့ 30-second API response cache သုံးမျိုးကို command များဖြင့်ရေးပါ။

You'll know it worked when: Atomic counter၊ expiring verification code နှင့် five-minute cache key ရမည်။

Strings၊ Counters၊ EXPIRE နှင့် TTL | Thuta Learning