MemoryStoreQueue

แสดงที่เลิกใช้งานแล้ว

*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่

ไม่สามารถสร้าง
ไม่ซ้ำ

ให้การเข้าถึงคิวภายใน MemoryStoreคิวเป็นโครงสร้างข้อมูลที่ให้พื้นที่เก็บชั่วคราวสำหรับไอเทมสุ่ม (ถึงขนาดไอเทมสูงสุด -- ดู ขีดจํากัดของ MemoryStore )แต่ละรายการคิวมีลําดับความสําคัญเป็นตัวเลข: MemoryStore ดึงรายการที่มีลําดับความสําคัญสูงกว่าจากคิวก่อน และดึงรายการที่มีลําดับความสําคัญเดียวกันตามลําดับเพิ่มขึ้น

รายการในคิวสามารถตั้งค่าให้หมดอายุได้หลังจากระยะเวลาหนึ่งหลังจากนั้นรายการที่หมดอายุจะหายไปจากคิวเหมือนว่าไม่เคยถูกเพิ่มเข้ามา

สรุป

วิธีการ

  • AddAsync(value : Variant,expiration : number,priority : number):()
    ผลตอบแทน

    เพิ่มรายการไปยังคิว

  • GetSizeAsync(excludeInvisible : boolean):number
    ผลตอบแทน

    รับขนาดของคิว

  • ReadAsync(count : number,allOrNothing : boolean,waitTimeout : number):Tuple
    ผลตอบแทน

    อ่านหนึ่งหรือมากกว่าหนึ่งรายการจากคิว

  • ผลตอบแทน

    ลบรายการหรือรายการที่อ่านไปแล้วจากคิว

คุณสมบัติ

วิธีการ

AddAsync

()
ผลตอบแทน

เพิ่มรายการไปยังคิว

พารามิเตอร์

value: Variant

มูลค่าของรายการที่จะเพิ่มลงในคิว

ค่าเริ่มต้น: ""
expiration: number

เวลาหมดอายุของรายการในวินาทีหลังจากนั้นรายการจะถูกลบออกจากคิวโดยอัตโนมัติ

ค่าเริ่มต้น: ""
priority: number

ลําดับความสําคัญของรายการ รายการที่มีความสําคัญสูงกว่าจะได้รับจากคิวก่อนรายการที่มีความสําคัญต่ํากว่า

ค่าเริ่มต้น: 0

ส่งค่ากลับ

()

GetSizeAsync

ผลตอบแทน

รับขนาดของคิว

พารามิเตอร์

excludeInvisible: boolean

กำหนดว่าจะเอาไอเทมที่มองไม่เห็นออกจากการนับขนาดหรือไม่

ค่าเริ่มต้น: false

ส่งค่ากลับ

ReadAsync

ผลตอบแทน

อ่านหนึ่งหรือมากกว่ารายการจากคิวเป็นการดำเนินการอะตอมเดียว

วิธีนี้ไม่ได้ลบรายการที่ส่งคืนจากคิวโดยอัตโนมัติ แต่ทำให้พวกเขามองไม่เห็นสำหรับการเรียกอื่นๆ ของ ReadAsync ในช่วงระยะเวลาของการหมดอายุการไม่มองเห็นรายการต้องถูกลบออกจากคิวโดยเฉพาะด้วย MemoryStoreQueue:RemoveAsync() ก่อนที่เวลาหมดอายุการไม่มองเห็นจะหมดลงเวลาหมดอายุการมองไม่เห็นเริ่มต้นที่ 30 วินาที เว้นแต่จะมีค่าที่แตกต่างกันให้ใน MemoryStoreService:GetQueue()

พารามิเตอร์

count: number

จํานวนรายการที่จะอ่าน ค่าสูงสุดที่อนุญาตสําหรับพารามิเตอร์นี้คือ 100

ค่าเริ่มต้น: ""
allOrNothing: boolean

ควบคุมพฤติกรรมของวิธีในกรณีที่คิวมีไอเทมน้อยกว่า count รายการ: หากตั้งค่าเป็น false วิธีจะคืนรายการทั้งหมดที่มีอยู่; หากตั้งค่าเป็น true จะไม่คืนรายการค่าเริ่มต้นคือ false

ค่าเริ่มต้น: false
waitTimeout: number

ระยะเวลาในวินาทีที่วิธีจะรอถ้าจํานวนรายการที่จําเป็นไม่สามารถใช้ได้ทันทีในคิวการอ่านจะพยายามทุกๆ สองวินาทีในช่วงเวลานี้พารามิเตอร์นี้สามารถตั้งค่าเป็นศูนย์เพื่อระบุว่าไม่มีการรอหากพารามิเตอร์นี้ไม่ได้รับหรือตั้งค่าเป็น -1 เมธอดจะรออย่างไม่มีกำหนด

ค่าเริ่มต้น: -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:

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.

Using a MemoryStoreQueue

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() เพื่อระบุรายการที่จะลบหากเรียกหลังจากหมดเวลาการไม่ปรากฏตัวแล้ว การเรียกจะไม่มีผล

พารามิเตอร์

id: string

ระบุรายการที่จะลบ ใช้ค่าที่ส่งคืนโดย 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:

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.

Using a MemoryStoreQueue

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

อีเวนต์