MemoryStoreQueue

非推奨を表示

*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。

作成できません
複製されていません

MemoryStore 内のキューへのアクセスを提供します。キューは、任意のアイテムに一時的なストレージを提供するデータ構造で、最大アイテムサイズまで(メモリストア制限 を参照)。各キューアイテムには数字の優先度があります:メモリストアは最初に優先度の高いアイテムをキューから取得し、同じ優先度のアイテムを順に取得します。

キューにあるアイテムは、特定の期間後に期限切れに設定できます。期限切れのアイテムは、追加されたことがないかのようにキューから消えます。

概要

方法

  • AddAsync(value : Variant,expiration : number,priority : number):()
    イールド

    キューにアイテムを追加します。

  • ReadAsync(count : number,allOrNothing : boolean,waitTimeout : number):Tuple
    イールド

    キューから1つまたは複数のアイテムを読みます。

  • イールド

    以前にキューから読み込まれたアイテムまたはアイテムを削除します。

プロパティ

方法

AddAsync

()
イールド

キューにアイテムを追加します。

パラメータ

value: Variant

キューに追加するアイテムの値。

既定値: ""
expiration: number

アイテムの有効期限時間、秒、後にアイテムは自動的にキューから削除されます。

既定値: ""
priority: number

アイテムの優先度。優先度の高いアイテムは、低い優先度のアイテムよりもまずキューから取得されます。

既定値: 0

戻り値

()

ReadAsync

イールド

キューから1つまたは複数のアイテムを単一のアトミック操作として読み込みます。

このメソッドは、返されたアイテムを自動的にキューから削除しませんが、非表示期間の他の ReadAsync 呼び出しには非表示になります。アイテムは、透明化の制限時間が切れる前に、MemoryStoreQueue:RemoveAsync() でキューから明示的に削除されなければなりません。透明化時間制限は、MemoryStoreService:GetQueue() に異なる値が提供されない限り、30秒にデフォルトで設定されます。

パラメータ

count: number

既読むアイテムの数。このパラメータの最大許可値は 100 です。

既定値: ""
allOrNothing: boolean

キューに count アイテムが少ない場合、メソッドの動作を制御する: false に設定すると、利用可能なすべてのアイテムが返されます; true に設定すると、アイテムが返されない。デフォルト値は false です。

既定値: false
waitTimeout: number

メソッドが待つ時間、秒単位で、必要なアイテムの数がキューにすぐに入手できない場合。この期間中、読み込みは 2 秒ごとに試行されます。このパラメータは、待機を示さないようにゼロに設定できます。このパラメータが提供されていないか、-1 に設定されている場合、メソッドは無期限に待機します。

既定値: -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:

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

イベント