提供存取MemoryStore内的队列。 队列是一个数据结构,为任意物品提供临时存储(最大物品大小见“MemoryStore限制”)。 每个队列项目都有一个数字优先级:MemoryStore从最高优先级到最低优先级,从最高优先级到最低优先级,从最高优先级到最低优先级。
可选设置项目在队列中过期时间。过期项目只会在队列中消失,无需添加。
概要
方法
将一个项目添加到队列。
从队列中读取一个或多个项目。
移除以前从队列中读取的物品或物品。
属性
方法
AddAsync
将一个项目添加到队列。
参数
物品的价值,要添加到队列。
物品有效时间,在此后,该物品将被自动从队列中移除。
物品优先级。优先级更高的物品从队列中回收,优先级更低的物品从其他队列中回收。
返回
ReadAsync
作为单个原子操作,读取队列中的一个或多个项目。
此方法不会自动从队列中删除返回的项目,但使其在隐身时间过期期间对其他 ReadAsync 调用隐形。该项目必须通过 MemoryStoreQueue:RemoveAsync() 在队列中被删除,才能在隐身时间过期后恢复可见。隐身时间默认为 30 秒,除非另一个值在 MemoryStoreService:GetQueue() 中提供了
参数
要已读取的项目数量。该参数的最大允许值为 100。
控制方法在队列中有少于 count 个物品的情况下的行为:如果设置为 false ,方法将返回所有可用物品;如果设置为 true ,将返回无物品。默认值为 false。
如果需要的物品数量不即可用,方法将在秒后等待。在此期间,每两秒钟尝试读取一次。此参数可以设置为零来表示无需等待。如果此参数未提供或设置为 -1,方法将在无法用于的时间。
返回
两个元素的 tuple 。第一个元素是从队列中读取的项目值阵列。第二个元素是一个串 ID ,应该传递给 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