MemoryStoreQueue
*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。
メモリストア内のキューにアクセスできます。キューは、Random アイテムの一時的なストレージを提供するデータ構造です (最大アイテムサイズ参照 - 「メモリストアの限界」を参照) 。各キューアイテムには、数値優先度があります: メモリストアは、最初にキューからより高い優先度のアイテムを取得し、その後、同じ優先
キューにあるアイテムは、一定の期間後にオプションで有効期限を設定できます。有効期限が切れたアイテムは、新しく追加されたときのようにキューから削除されます。
概要
方法
キューにアイテムを追加します。
キューから 1 つまたは複数のアイテムを読み取ります。
以前にキューから読み込んだアイテムまたはアイテムを削除します。
プロパティ
方法
AddAsync
キューにアイテムを追加します。
パラメータ
キューに追加するアイテムの値。
アイテムの有効期限、秒単位です。この後、アイテムは自動的にキューから削除されます。
アイテムの優先度。優先度の高いアイテムは、優先度の低いアイテムよりも早くキューから取得されます。
戻り値
ReadAsync
キューから 1 つまたは複数のアイテムを単一のアトミックオペレーションとして読み取ります。
このメソッドは、返されたアイテムをクイーに自動的に削除することはありませんが、MemoryStoreQueue:RemoveAsync() の他の ReadAsync コールの期間、アイテムを不可視にします。アイテムは、MemoryStoreService:GetQueue() の前に、Class.MemoryService:GetQueue() で指定さ
パラメータ
既読み込むアイテムの数。このパラメータの最大許可値は 100 です。
キューに count がある場合、メソッドの動作をコントロールします: if set to false the method returns all available items; if set to true, it returns no items. The default value is false。
必要なアイテムの数がすぐに利用可能でない場合、メソッドは何秒で待ちますか。この期間中、2秒ごとに読み取りが試行されます。このパラメーターは、「待ち」を指定するために 0 に設定できます。このパラメーターが 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