MemoryStoreQueue
*Ta zawartość została przetłumaczona przy użyciu narzędzi AI (w wersji beta) i może zawierać błędy. Aby wyświetlić tę stronę w języku angielskim, kliknij tutaj.
Zapewnia dostęp do kolejki w MemoryStore.Kolejka to struktura danych, która zapewnia tymczasowe przechowywanie dowolnych przedmiotów (do maksymalnej wielkości przedmiotu -- patrz Ograniczenia przechowywania pamięci).Każdy element kolejki ma priorytet liczbowy: MemoryStore najpierw odzyskuje przedmioty o wyższym priorytecie z kolejki, a następnie odzyskuje przedmioty o tym samym priorytecie w kolejności dodania.
Przedmioty w kolejce mogą opcjonalnie wygasnąć po pewnym czasie.Wygasłe przedmioty po prostu znikają z kolejki, tak jakby nigdy nie zostały dodane.
Podsumowanie
Metody
Dodaje przedmiot do kolejki.
Czyta jeden lub więcej przedmiotów z kolejki.
Usuwa przedmiot lub przedmioty wcześniej przeczytane z kolejki.
Właściwości
Metody
AddAsync
Dodaje przedmiot do kolejki.
Parametry
Wartość przedmiotu do dodania do kolejki.
Czas wygaśnięcia przedmiotu, w sekundach, po którym przedmiot zostanie automatycznie usunięty z kolejki.
Priorytet przedmiotu. Przedmioty o wyższym priorytecie są pobierane z kolejki przed przedmiotami o niższym priorytecie.
Zwroty
ReadAsync
Czyta jeden lub więcej elementów z kolejki jako pojedynczą operację atomową.
Ta metoda nie automatycznie usuwa zwrócone przedmioty z kolejki, ale czyni je niewidocznymi dla innych wezwań ReadAsync na okres wygaśnięcia niewidzialności.Przedmioty muszą być wyraźnie usunięte z kolejki za pomocą MemoryStoreQueue:RemoveAsync() przed wygaśnięciem czasu oczekiwania na niewidzialność.Czas wygaśnięcia niewidzialności domyślnie wynosi 30 sekund, chyba że dostarczono inną wartość w MemoryStoreService:GetQueue().
Parametry
Liczba elementów do przeczytane. Maksymalna dozwolona wartość tego parametru wynosi 100.
Kontroluje zachowanie metody w przypadku, gdy kolejka ma mniej niż count elementów: jeśli ustawiono na fałd, metoda zwraca wszystkie dostępne elementy; jeśli ustawiono na prawdę, nie zwraca żadnych elementów.Domyślna wartość to fałsz.
Czas, w sekundach, na który czeka metoda, jeśli wymagana liczba przedmiotów nie jest dostępna natychmiast w kolejce.Przeczytania są próbowane co dwie sekundy w tym okresie.Ten parametr można ustawić na zero, aby wskazać brak oczekiwania.Jeśli ten parametr nie zostanie podany lub ustawiony na -1, metoda będzie czekać nieokreślony czas.
Zwroty
Tupla dwóch elementów.Pierwszy element to lista wartości przedmiotów odczytanych z kolejki.Drugim elementem jest identyfikator ciągu, który należy przekazać do MemoryStoreQueue:RemoveAsync(), aby na stałe usunąć te przedmioty z kolejki.
Przykłady kodu
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
Usuwa przedmiot lub przedmioty wcześniej przeczytane z kolejki.Ta metoda wykorzystuje identyfikator zwrotny przez MemoryStoreQueue:ReadAsync() do identyfikacji elementów do usuwać, wyjmować.Jeśli wezwanie zostanie wywołane po wygaśnięciu czasu wyłączenia niewidzialności, wezwanie nie będzie miało żadnego skutku.
Parametry
Określa przedmioty do usuwać. Użyj wartości zwrotnej przez MemoryStoreQueue:ReadAsync().
Zwroty
Przykłady kodu
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