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

Lua Scripts နှင့် Redis Functions

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

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

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

Lua script က commands အများကို server ပေါ်မှာ atomic unit တစ်ခုအဖြစ် run လုပ်ပေးပါတယ်။ Keys ကို `KEYS[]`၊ values ကို `ARGV[]` ဖြင့်ပေးပြီး cluster routing အတွက် accessed keys အားလုံးကြေညာရပါတယ်။ Long-running script က server ကို block လုပ်နိုင်လို့ bounded work သာလုပ်ရပါတယ်။ Redis Functions က named, persisted library အဖြစ် logic ကို deploy နိုင်ပါတယ်။

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

Limited coupon claim ကို remaining counter စစ်၊ decrement လုပ်ပြီး user claim set ထဲထည့်မယ်။ Duplicate claim ကို no-op၊ stock zero ကို reject လုပ်မယ်။ Script SHA cache miss တွင် `NOSCRIPT` ရလျှင် load/retry လုပ်ပြီး business outcome codes ကို metrics မှတ်မယ်။

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

lua
if redis.call('SISMEMBER', KEYS[2], ARGV[1]) == 1 then return 2 end
local remaining = tonumber(redis.call('GET', KEYS[1]) or '0')
if remaining <= 0 then return 0 end
redis.call('DECR', KEYS[1])
redis.call('SADD', KEYS[2], ARGV[1])
return 1

-- KEYS: coupon:{42}:remaining, coupon:{42}:claims
You should see
0=sold out၊ 1=claimed၊ 2=duplicate ကို atomic ပြန်ပေးမည်။

၅ မိနစ် စမ်းကြည့်

Rate limiter script ကို success/blocked/error outcome codes နဲ့ test cases ရေးပါ။

သတိလေးတစ်ချက်

Lua ထဲ unbounded SCAN/loop သို့ external call မလုပ်ပါနှင့်။ Server event loop ကို block လုပ်နိုင်ပါတယ်။

Redis — Scripting with LuaRedis

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

  • Lua ထဲ unbounded SCAN/loop သို့ external call မလုပ်ပါနှင့်။ Server event loop ကို block လုပ်နိုင်ပါတယ်။
  • နမူနာ command ကို production Redis ပေါ် တိုက်ရိုက်မစမ်းဘဲ local/test instance နဲ့ recoverable data ပေါ်တွင် အရင်အတည်ပြုပါ။

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

Rate limiter script ကို success/blocked/error outcome codes နဲ့ test cases ရေးပါ။

You'll know it worked when: 0=sold out၊ 1=claimed၊ 2=duplicate ကို atomic ပြန်ပေးမည်။

Lua Scripts နှင့် Redis Functions | Thuta Learning