提供在記憶體儲存中存取隊列的訪問。隊列是一種提供暫時存儲的數據結構,用於任意項目(直到最大項目尺寸--參見記憶儲存限制)。每個隊列項目都有一個數字優先級:MemoryStore 首先從隊列中擷取優先級更高的項目,然後按順序擷取相同優先級的項目。
隊列中的項目可選擇在一定時間後過期。過期的項目會從隊列中消失,好像從來沒有被添加過一樣。
概要
方法
添加一個項目到隊列。
取得隊列的大小。
從隊列中讀取一個或多個項目。
從隊列中移除一個或多個先前已閱讀的項目。
屬性
方法
AddAsync
添加一個項目到隊列。
參數
要添加到隊列的項目值。
項目過期時間,以秒為單位,過期後項目將自動從隊列中移除。
項目優先級。優先級更高的項目會先從隊列中取得,而優先級較低的項目會先於隊列中取得。
返回
ReadAsync
以單一原子操作閱讀隊列中的一個或多個項目。
此方法不會自動刪除返回的項目從隊列中,但在隱形時間期間讓它們對其他的 ReadAsync 呼叫不可見。項目必須在隱形時間限制到期之前明確從隊列中移除MemoryStoreQueue:RemoveAsync(),否則將無法使用。隱形時間超時默認為 30 秒,除非在 MemoryStoreService:GetQueue() 中提供了不同值。
參數
要閱讀的項目數量。此參數的最大允許值為 100。
在隊列中少於 count 項目的情況下控制方法的行為:如果設為 false,方法返回所有可用項目;如果設為 true,則不返回任何項目。預設值為 false。
方法在必要數量的項目無法立即在隊列中獲得時,會等待的時間(秒)。在此期間每兩秒鐘嘗試讀取一次。此參數可設為零以表示沒有等待。如果此參數未提供或設為 -1,方法將永久等待。
返回
一個包含兩個元素的字元組。第一個元素是從隊列中閱讀的項目值數組。第二個元素是一個字串標示符,應該傳送到 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