MemoryStoreQueue

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

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

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

ให้การเข้าถึงคิวในหน่วยความจำ คิวเป็นโครงสร้างข้อมูลที่ให้การจัดเก็บข้อมูลชั่วคราวสำหรับรายการใด ๆ (เช่นเดียวกับขนาดรายการสูงสุด -- ดู ขีดจำกัดของหน่

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

สรุป

วิธีการ

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

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

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

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

  • RemoveAsync(id : string):void
    ผลตอบแทน

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

คุณสมบัติ

วิธีการ

AddAsync

void
ผลตอบแทน

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

พารามิเตอร์

value: Variant

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

expiration: number

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

priority: number

ความสามารถของรายการ รายการที่มีความสามารถสูงสุดจะดึงจากคิวก่อนรายการที่มีความสามารถต่ำสุด

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

ส่งค่ากลับ

void

ReadAsync

ผลตอบแทน

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

วิธีนี้ไม่ได้ลบรายการที่กลับมาจากคิวโดยอัตโนมัติ แต่ทำให้พวกเขามองไม่เห็นสำหรับการโอนย้าย ReadAsync สำหรับระยะเวลาของความไม่มองเห็น ราย

พารามิเตอร์

count: number

อ่านแล้วมูลค่าสูงสุดที่อนุญาตของตัวนี้คือ 100

allOrNothing: bool

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

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

ระยะเวลาในวินาทีที่วิธีจะรอหากจำนวนรายการที่ต้องการไม่สามารถใช้งานได้ทันทีในคิว การอ่านจะพยายามทุกสองวินาทีในระยะเวลานี้ ตัวแปรนี้สามารถตั้งค่าได้เป็น 0 เพื่อแสดง

ค่าเริ่มต้น: -1

ส่งค่ากลับ

ตัวแปรสององค์ประกอบ องค์ประกอบแรกคือ 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:

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

void
ผลตอบแทน

ลบรายการหรือรายการที่อ่านมาก่อนจากคิว วิธีนี้ใช้ตัวระบุที่กลับมาจาก MemoryStoreQueue:ReadAsync() เพื่อระบุรายการที่จะลบ หากโทรหลังจากหมดเวลาในข้อความอินทรารัศมี การโทรจะไม่มีผล

พารามิเตอร์

id: string

ระบุรายการที่จะลบ ใช้ค่าที่กลับมาโดย MemoryStoreQueue:ReadAsync()


ส่งค่ากลับ

void

ตัวอย่างโค้ด

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

อีเวนต์