MemoryStoreQueue
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
ให้การเข้าถึงคิวภายใน MemoryStoreคิวเป็นโครงสร้างข้อมูลที่ให้พื้นที่เก็บชั่วคราวสำหรับไอเทมสุ่ม (ถึงขนาดไอเทมสูงสุด -- ดู ขีดจํากัดของ MemoryStore )แต่ละรายการคิวมีลําดับความสําคัญเป็นตัวเลข: MemoryStore ดึงรายการที่มีลําดับความสําคัญสูงกว่าจากคิวก่อน และดึงรายการที่มีลําดับความสําคัญเดียวกันตามลําดับเพิ่มขึ้น
รายการในคิวสามารถตั้งค่าให้หมดอายุได้หลังจากระยะเวลาหนึ่งหลังจากนั้นรายการที่หมดอายุจะหายไปจากคิวเหมือนว่าไม่เคยถูกเพิ่มเข้ามา
สรุป
วิธีการ
เพิ่มรายการไปยังคิว
รับขนาดของคิว
อ่านหนึ่งหรือมากกว่าหนึ่งรายการจากคิว
ลบรายการหรือรายการที่อ่านไปแล้วจากคิว
คุณสมบัติ
วิธีการ
AddAsync
เพิ่มรายการไปยังคิว
พารามิเตอร์
มูลค่าของรายการที่จะเพิ่มลงในคิว
เวลาหมดอายุของรายการในวินาทีหลังจากนั้นรายการจะถูกลบออกจากคิวโดยอัตโนมัติ
ลําดับความสําคัญของรายการ รายการที่มีความสําคัญสูงกว่าจะได้รับจากคิวก่อนรายการที่มีความสําคัญต่ํากว่า
ส่งค่ากลับ
GetSizeAsync
รับขนาดของคิว
พารามิเตอร์
กำหนดว่าจะเอาไอเทมที่มองไม่เห็นออกจากการนับขนาดหรือไม่
ส่งค่ากลับ
ReadAsync
อ่านหนึ่งหรือมากกว่ารายการจากคิวเป็นการดำเนินการอะตอมเดียว
วิธีนี้ไม่ได้ลบรายการที่ส่งคืนจากคิวโดยอัตโนมัติ แต่ทำให้พวกเขามองไม่เห็นสำหรับการเรียกอื่นๆ ของ ReadAsync ในช่วงระยะเวลาของการหมดอายุการไม่มองเห็นรายการต้องถูกลบออกจากคิวโดยเฉพาะด้วย MemoryStoreQueue:RemoveAsync() ก่อนที่เวลาหมดอายุการไม่มองเห็นจะหมดลงเวลาหมดอายุการมองไม่เห็นเริ่มต้นที่ 30 วินาที เว้นแต่จะมีค่าที่แตกต่างกันให้ใน MemoryStoreService:GetQueue()
พารามิเตอร์
จํานวนรายการที่จะอ่าน ค่าสูงสุดที่อนุญาตสําหรับพารามิเตอร์นี้คือ 100
ควบคุมพฤติกรรมของวิธีในกรณีที่คิวมีไอเทมน้อยกว่า count รายการ: หากตั้งค่าเป็น false วิธีจะคืนรายการทั้งหมดที่มีอยู่; หากตั้งค่าเป็น true จะไม่คืนรายการค่าเริ่มต้นคือ false
ระยะเวลาในวินาทีที่วิธีจะรอถ้าจํานวนรายการที่จําเป็นไม่สามารถใช้ได้ทันทีในคิวการอ่านจะพยายามทุกๆ สองวินาทีในช่วงเวลานี้พารามิเตอร์นี้สามารถตั้งค่าเป็นศูนย์เพื่อระบุว่าไม่มีการรอหากพารามิเตอร์นี้ไม่ได้รับหรือตั้งค่าเป็น -1 เมธอดจะรออย่างไม่มีกำหนด
ส่งค่ากลับ
tuple ของสององค์ประกอบองค์ประกอบแรกคือค่าตัวรายการที่อ่านจากคิวองค์ประกอบที่สองคือตัวระบุสตริงที่ควรส่งไปยัง 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