提供存取MemoryStore的隊列。 隊列是一種資料結構,可以為任意項目提供暫時存取(最大項目大小見 MemoryStore限制)。 每個隊列項目都有一個數字優先權:MemoryStore會先從隊列中取回更高優先權的項目,然後再從同一優先權的項目取回它們。
順序中的項目可以選擇在特定時間後過期。過期的項目會像從未添加過一樣從順序中消失。
概要
方法
添加一個項目到清單。
從佇列中閱取一個或多個項目。
移除以前從佇列中閱取的項目或項目。
屬性
方法
AddAsync
添加一個項目到清單。
參數
項目的價值,要加到清單中。
項目的過期時間,在秒後,項目將自動從排隊中移除。
項目優先權。優先權更高的項目從排隊中擷取,優先權更低的項目從其他項目中擷取。
返回
ReadAsync
以原子操作單一或多個項目從佇列中讀取。
此方法不會自動從返回的項目從排隊中刪除,但使其在隱形期間的其他 ReadAsync 呼叫中不可用視。項目必須從排隊中通過 MemoryStoreQueue:RemoveAsync() 以顯示在隱形時間到期前無法被其他 ReadAsync 呼叫。 隱形時間預設為 30 秒,除非另一個值在 Class.MemoryService:
參數
要閱已讀的項目數量。此參數的最大允許值是 100。
控制方法在排隊擁有少於 count 個項目的情況下的行為:如果設為 false,方法將返回所有可用項目;如果設為 true,將返回 no 項目。預設值為 false。
方法將在隊列中立即沒有可用的項目數量,如果此數量不是立即可用的,方法將在秒鐘後等待。閱取每兩秒進行一次。此參數可設為零,以表示沒有等待。如果此參數未設為 -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:
- 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