MemoryStoreQueue

非推奨を表示

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

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

メモリストア内のキューにアクセスできます。キューは、Random アイテムの一時的なストレージを提供するデータ構造です (最大アイテムサイズ参照 - 「メモリストアの限界」を参照) 。各キューアイテムには、数値優先度があります: メモリストアは、最初にキューからより高い優先度のアイテムを取得し、その後、同じ優先

キューにあるアイテムは、一定の期間後にオプションで有効期限を設定できます。有効期限が切れたアイテムは、新しく追加されたときのようにキューから削除されます。

概要

方法

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

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

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

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

  • RemoveAsync(id : string):void
    イールド

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

プロパティ

方法

AddAsync

void
イールド

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

パラメータ

value: Variant

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

expiration: number

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

priority: number

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

既定値: 0

戻り値

void

ReadAsync

イールド

キューから 1 つまたは複数のアイテムを単一のアトミックオペレーションとして読み取ります。

このメソッドは、返されたアイテムをクイーに自動的に削除することはありませんが、MemoryStoreQueue:RemoveAsync() の他の ReadAsync コールの期間、アイテムを不可視にします。アイテムは、MemoryStoreService:GetQueue() の前に、Class.MemoryService:GetQueue() で指定さ

パラメータ

count: number

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

allOrNothing: bool

キューに count がある場合、メソッドの動作をコントロールします: if set to false the method returns all available items; if set to true, it returns no items. The default value is false。

既定値: false
waitTimeout: number

必要なアイテムの数がすぐに利用可能でない場合、メソッドは何秒で待ちますか。この期間中、2秒ごとに読み取りが試行されます。このパラメーターは、「待ち」を指定するために 0 に設定できます。このパラメーターが 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

void
イールド

以前にキューから読み込まれたアイテムやアイテムを削除します。このメソッドは、MemoryStoreQueue:ReadAsync() によって返された識別子を使用して、削除するアイテムを識別します。無効になったインビジビリティタイマウトの後に呼び出されると、呼び出しは影響を与えません。

パラメータ

id: string

削除するアイテムを識別します。 MemoryStoreQueue:ReadAsync() によって返される値を使用してください。


戻り値

void

コードサンプル

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

イベント