MemoryStoreQueue
*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。
MemoryStore 内のキューへのアクセスを提供します。キューは、任意のアイテムに一時的なストレージを提供するデータ構造で、最大アイテムサイズまで(メモリストア制限 を参照)。各キューアイテムには数字の優先度があります:メモリストアは最初に優先度の高いアイテムをキューから取得し、同じ優先度のアイテムを順に取得します。
キューにあるアイテムは、特定の期間後に期限切れに設定できます。期限切れのアイテムは、追加されたことがないかのようにキューから消えます。
概要
方法
キューにアイテムを追加します。
キューから1つまたは複数のアイテムを読みます。
以前にキューから読み込まれたアイテムまたはアイテムを削除します。
プロパティ
方法
AddAsync
キューにアイテムを追加します。
パラメータ
キューに追加するアイテムの値。
アイテムの有効期限時間、秒、後にアイテムは自動的にキューから削除されます。
アイテムの優先度。優先度の高いアイテムは、低い優先度のアイテムよりもまずキューから取得されます。
戻り値
ReadAsync
キューから1つまたは複数のアイテムを単一のアトミック操作として読み込みます。
このメソッドは、返されたアイテムを自動的にキューから削除しませんが、非表示期間の他の ReadAsync 呼び出しには非表示になります。アイテムは、透明化の制限時間が切れる前に、MemoryStoreQueue:RemoveAsync() でキューから明示的に削除されなければなりません。透明化時間制限は、MemoryStoreService:GetQueue() に異なる値が提供されない限り、30秒にデフォルトで設定されます。
パラメータ
既読むアイテムの数。このパラメータの最大許可値は 100 です。
キューに count アイテムが少ない場合、メソッドの動作を制御する: false に設定すると、利用可能なすべてのアイテムが返されます; true に設定すると、アイテムが返されない。デフォルト値は false です。
メソッドが待つ時間、秒単位で、必要なアイテムの数がキューにすぐに入手できない場合。この期間中、読み込みは 2 秒ごとに試行されます。このパラメータは、待機を示さないようにゼロに設定できます。このパラメータが提供されていないか、-1 に設定されている場合、メソッドは無期限に待機します。
戻り値
2つの要素のツープル。最初の要素は、キューから読み込まれたアイテム値の配列です。2番目の要素は、これらのアイテムを永久にキューから削除するために 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