MemoryStoreQueue
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
ให้การเข้าถึงคิวในหน่วยความจำ คิวเป็นโครงสร้างข้อมูลที่ให้การจัดเก็บข้อมูลชั่วคราวสำหรับรายการใด ๆ (เช่นเดียวกับขนาดรายการสูงสุด -- ดู ขีดจำกัดของหน่
รายการในคิวสามารถตั้งค่าได้ว่าจะหมดอายุหลังจากระยะเวลาที่กำหนด รายการที่หมดอายุจะเพิ่มขึ้นจากคิวเช่นเดียวกับที่พวกเขาไม่เคยเพิ่ม
สรุป
วิธีการ
เพิ่มรายการไอเท็มไปยังคิว
อ่านหนึ่งหรือมากกว่ารายการจากคิว
ลบรายการหรือรายการที่อ่านไปแล้วจากคิว
คุณสมบัติ
วิธีการ
AddAsync
เพิ่มรายการไอเท็มไปยังคิว
พารามิเตอร์
มูลค่าของรายการที่จะเพิ่มไปยังคิว
เวลาหมดอายุของรายการในวินาทีหลังจากนั้นรายการจะถูกลบออกจากคิวโดยอัตโนมัติ
ความสามารถของรายการ รายการที่มีความสามารถสูงสุดจะดึงจากคิวก่อนรายการที่มีความสามารถต่ำสุด
ส่งค่ากลับ
ReadAsync
อ่านรายการหนึ่งหรือมากกว่าจากคิวเป็นการดำเนินงานอะตอมเดียว
วิธีนี้ไม่ได้ลบรายการที่กลับมาจากคิวโดยอัตโนมัติ แต่ทำให้พวกเขามองไม่เห็นสำหรับการโอนย้าย ReadAsync สำหรับระยะเวลาของความไม่มองเห็น ราย
พารามิเตอร์
อ่านแล้วมูลค่าสูงสุดที่อนุญาตของตัวนี้คือ 100
ควบคุมพฤติกรรมของวิธีในกรณีที่คิวมีน้อยกว่า count รายการ: หากตั้งค่าไปยังปลอมวิธีจะคืนรายการทั้งหมดที่มี; หากตั้งค่าไปที่จริงวิธีจะไม่คืนรายการใด ๆ
ระยะเวลาในวินาทีที่วิธีจะรอหากจำนวนรายการที่ต้องการไม่สามารถใช้งานได้ทันทีในคิว การอ่านจะพยายามทุกสองวินาทีในระยะเวลานี้ ตัวแปรนี้สามารถตั้งค่าได้เป็น 0 เพื่อแสดง
ส่งค่ากลับ
ตัวแปรสององค์ประกอบ องค์ประกอบแรกคือ mat阵ของมูลค่ารายการที่อ่านจากคิว องค์ประกอบที่สองคือตัวระบุสตริงที่ควรถูกส่งให้กับ MemoryStoreQueue:RemoveAsync() เพื่อถาวรลบรายการเหล่านี้ออกจากคิว
ตัวอย่างโค้ด
The following code sample demonstrates using MemoryStoreQueue:ReadAsync() and MemoryStoreQueue:RemoveAsync() to reliably read, process, and remove items from a queue. Though this process can be as complicated as necessary, this example simply sets a flag in the corresponding data store item, which guarantees that every item will eventually be processed even if some of the calls encounter errors or the server crashes:
- If MemoryStoreQueue:ReadAsync() fails, no items are retrieved from the queue. An item will be picked up for processing during the next iteration of the loop.
- If MemoryStoreQueue:ReadAsync() succeeds but DataStoreService:UpdateAsync() fails, the item stays invisible until the queue's invisibility timeout expires, causing the item becoming visible again to be returned in a future loop iteration.
- Similarly, if MemoryStoreQueue:RemoveAsync() fails, the item will become visible again in the future and will be processed again.
Depending on where the failure happens, it's possible that an item will be processed more than once. You should account for that like the following code sample, in which the end result is the same even if DataStoreService:UpdateAsync() is invoked multiple times.
local MemoryStoreService = game:GetService("MemoryStoreService")
local DataStoreService = game:GetService("DataStoreService")
local queue = MemoryStoreService:GetQueue("PlayerQueue")
local dataStore = DataStoreService:GetDataStore("PlayerStore")
while true do
pcall(function()
-- wait for an item to process
local items, id = queue:ReadAsync(1, false, 30)
-- check if an item was retrieved
if #items > 0 then
-- mark the item as processed
dataStore:UpdateAsync(items[0], function(data)
data = data or {}
data.processed = 1
return data
end)
-- remove the item from the queue
queue:RemoveAsync(id)
end
end)
end
RemoveAsync
ลบรายการหรือรายการที่อ่านมาก่อนจากคิว วิธีนี้ใช้ตัวระบุที่กลับมาจาก MemoryStoreQueue:ReadAsync() เพื่อระบุรายการที่จะลบ หากโทรหลังจากหมดเวลาในข้อความอินทรารัศมี การโทรจะไม่มีผล
พารามิเตอร์
ระบุรายการที่จะลบ ใช้ค่าที่กลับมาโดย MemoryStoreQueue:ReadAsync()
ส่งค่ากลับ
ตัวอย่างโค้ด
The following code sample demonstrates using MemoryStoreQueue:ReadAsync() and MemoryStoreQueue:RemoveAsync() to reliably read, process, and remove items from a queue. Though this process can be as complicated as necessary, this example simply sets a flag in the corresponding data store item, which guarantees that every item will eventually be processed even if some of the calls encounter errors or the server crashes:
- If MemoryStoreQueue:ReadAsync() fails, no items are retrieved from the queue. An item will be picked up for processing during the next iteration of the loop.
- If MemoryStoreQueue:ReadAsync() succeeds but DataStoreService:UpdateAsync() fails, the item stays invisible until the queue's invisibility timeout expires, causing the item becoming visible again to be returned in a future loop iteration.
- Similarly, if MemoryStoreQueue:RemoveAsync() fails, the item will become visible again in the future and will be processed again.
Depending on where the failure happens, it's possible that an item will be processed more than once. You should account for that like the following code sample, in which the end result is the same even if DataStoreService:UpdateAsync() is invoked multiple times.
local MemoryStoreService = game:GetService("MemoryStoreService")
local DataStoreService = game:GetService("DataStoreService")
local queue = MemoryStoreService:GetQueue("PlayerQueue")
local dataStore = DataStoreService:GetDataStore("PlayerStore")
while true do
pcall(function()
-- wait for an item to process
local items, id = queue:ReadAsync(1, false, 30)
-- check if an item was retrieved
if #items > 0 then
-- mark the item as processed
dataStore:UpdateAsync(items[0], function(data)
data = data or {}
data.processed = 1
return data
end)
-- remove the item from the queue
queue:RemoveAsync(id)
end
end)
end