MemoryStoreQueue

顯示已棄用項目

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡

無法建立
未複製

提供存取MemoryStore的隊列。 隊列是一種資料結構,可以為任意項目提供暫時存取(最大項目大小見 MemoryStore限制)。 每個隊列項目都有一個數字優先權:MemoryStore會先從隊列中取回更高優先權的項目,然後再從同一優先權的項目取回它們。

順序中的項目可以選擇在特定時間後過期。過期的項目會像從未添加過一樣從順序中消失。

概要

方法

屬性

方法

AddAsync

void
暫停

添加一個項目到清單。

參數

value: Variant

項目的價值,要加到清單中。

expiration: number

項目的過期時間,在秒後,項目將自動從排隊中移除。

priority: number

項目優先權。優先權更高的項目從排隊中擷取,優先權更低的項目從其他項目中擷取。

預設值:0

返回

void

ReadAsync

暫停

以原子操作單一或多個項目從佇列中讀取。

此方法不會自動從返回的項目從排隊中刪除,但使其在隱形期間的其他 ReadAsync 呼叫中不可用視。項目必須從排隊中通過 MemoryStoreQueue:RemoveAsync() 以顯示在隱形時間到期前無法被其他 ReadAsync 呼叫。 隱形時間預設為 30 秒,除非另一個值在 Class.MemoryService:

參數

count: number

要閱已讀的項目數量。此參數的最大允許值是 100。

allOrNothing: bool

控制方法在排隊擁有少於 count 個項目的情況下的行為:如果設為 false,方法將返回所有可用項目;如果設為 true,將返回 no 項目。預設值為 false。

預設值:false
waitTimeout: number

方法將在隊列中立即沒有可用的項目數量,如果此數量不是立即可用的,方法將在秒鐘後等待。閱取每兩秒進行一次。此參數可設為零,以表示沒有等待。如果此參數未設為 -1,方法將在無法使用。

預設值:-1

返回

兩個元素的範例。第一個元素是從隨機值陣中閱取的項目值。第二個元素是一個永久從隨機值陣中移除這些項目的字串標識符。

範例程式碼

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

活動