MemoryStoreQueue

显示已弃用

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

无法创建
未复制

提供在 MemoryStore 中访问队列的功能。队列是一个数据结构,为任意项目提供临时存储(最大项目尺寸见 内存存储限制)。每个队列项目都有一个数字优先级:MemoryStore首先从队列中检索优先级更高的项目,然后按顺序检索相同优先级的项目。

队列中的项目可以选择在一定时间后过期。过期的项目简单地从队列中消失,如果它们从来没有被添加。

概要

方法

属性

方法

AddAsync

()
暂停

将一件物品添加到队列。

参数

value: Variant

要添加到队列的物品的值。

默认值:""
expiration: number

物品有效期时间,以秒为单位,过期后该物品将自动从队列中删除。

默认值:""
priority: number

项目优先级。优先级更高的项目从队列中提取,优先级更低的项目在队列中排在前面。

默认值:0

返回

()

ReadAsync

暂停

将队列中的一个或多个项目读为单个原子操作。

该方法不会自动删除返回的项目从队列中,但在隐形期间使其对其他读取异步调用不可见。项目必须在隐形时间过期前明确从队列中删除 MemoryStoreQueue:RemoveAsync() ,否则将无法使用。隐形时间超时默认为 30 秒,除非 MemoryStoreService:GetQueue() 中提供了不同的值。

参数

count: number

要阅已读的项目数量。该参数的最大允许值为 100。

默认值:""
allOrNothing: boolean

在队列中少于 count 项的情况下控制方法的行为:如果设置为 false,方法返回所有可用的项目;如果设置为 true,则不返回任何项目。默认值为 false。

默认值:false
waitTimeout: number

方法在队列中没有所需数量的项目时,会等待的时间(秒)。在此期间,每两秒尝试阅读一次。该参数可以设置为零以表示没有等待。如果此参数未提供或设置为 -1,方法将永久等待。

默认值:-1

返回

两个元素的 tuple。第一个元素是从队列中读取的一组项目值阵列。第二个元素是一个字符串标识符,应该传给 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

()
暂停

从队列中删除一个或多个先前阅读的项目。该方法使用由 MemoryStoreQueue:ReadAsync() 返回的识别符来识别要删移除的项目。如果在隐形时间过期后调用,该调用没有效果。

参数

id: string

识别要删除的项目。使用 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:

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

活动