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.
Dostarcza dostęp do konsoli w MemoryStore. Konsola jest strukturą danych, która dostarcza tymczasowe przechowywanie dla dowolnych pozycji (maksymalny rozmiar pozycji – patrz Limitowanie MemoryStore). Każdy pozycji konsoli ma priorytet liczbowy: MemoryStore odczytuje pozycje z wyższym priorytetem z konsoli pierwszy, a odczytuje pozycje z tego samego priorytetu w kolejności dodawania.
Przedmioty w kolejce mogą być opcjonalnie ustawione na wygasanie po pewnym czasie. Wygasłe przedmioty po prostu znikają z kolejki, jakby nie zostały nigdy dodane.
Podsumowanie
Metody
Dodaje pozycję do kolejki.
Czyta jeden lub więcej pozycji z koszyku.
Usunie jeden lub więcej poprzednio czytanych pozycji z koszyka.
Właściwości
Metody
AddAsync
Dodaje pozycję do kolejki.
Parametry
Wartość przedmiotu do dodania do kolejki.
Czas wygasania przedmiotu, w sekundach, po którym przedmiot zostanie automatycznie usunięty z kodu.
Priorytet przedmiotu. Przedmioty o większym priorytecie są odczytywane z koszyku przedmiotów przed przedmiotami o niższym priorytecie.
Zwroty
ReadAsync
Czyta jeden lub więcej pojedynczych pozycji z kolejki jako jedną lub więcej operacji atomowych.
Ten metod nie automatycznie usuwa zwróconych pozycji z koszyka, ale sprawia, że są niewidoczne dla innych wezwania ReadAsync przez okres czasu wygasania niewidzialności. Pozycje muszą być wyraźnie usunięte z koszyka z MemoryStoreQueue:RemoveAsync() przed wygasнием czasu wygaszenia niewidzialności. Domyślny czas wygaszenia wynosi 30 se
Parametry
Liczba pozycji do przeczytane. Maksymalna wartość tego parametru to 100.
Kontroluje zachowanie metody w przypadku, gdy kolejka ma mniej niż count pozycji: jeśli ustawiono na fałszywy, metoda zwraca wszystkie dostępne pozycje; jeśli ustawiono na prawdziwy, nie zwraca żadnych pozycji. Domyślną wartością jest fałszywy.
Czas, w sekundach, dla którego metoda będzie czekać, jeśli wymaganą liczbę pozycji nie jest natychmiastowo dostępna w kolejce. Czytania są próbowane co dwa sekundy w tym okresie. Ten parametr można ustawić na zero, aby nie wykonywać czekania. Jeśli ten parametr nie jest ustawiony lub ustawiony na -1, metoda będzie czekać w nieokreślonym czasie.
Zwroty
Tup z dwóch elementów. Pierwszy element to maszyna przecinków czytanych z konsoli. Drugi element to identyfikator maszyny przecinków, który powinien być przekazany do MemoryStoreQueue:RemoveAsync() , aby stale usunąć te pozycje z konsoli.
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 czytane z konsoli. Ten metod używa identyfikatora zwróconego przez MemoryStoreQueue:ReadAsync(), aby zidentyfikować przedmioty do usuwać, wyjmować. Jeśli zostanie wywołany po wygasaniu czasu nieaktywności, wezwanie nie ma efektu.
Parametry
Określa przedmioty do usuwać. Użyj wartości zwróconej 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