MemoryStoreQueue
*Konten ini diterjemahkan menggunakan AI (Beta) dan mungkin mengandung kesalahan. Untuk melihat halaman ini dalam bahasa Inggris, klik di sini.
Memberikan akses ke antrian di dalam MemoryStore.Antrian adalah struktur data yang memberikan penyimpanan sementara untuk item arbitrer (hingga ukuran item maksimum -- lihat Batas Memori Toko).Setiap item antrian memiliki prioritas numerik: MemoryStore memulihkan item dengan prioritas lebih tinggi dari antrian terlebih dahulu, dan memulihkan item dengan prioritas yang sama dalam urutan tambahan.
Item di antrian dapat diatur untuk kedaluwarsa setelah jangka waktu tertentu.Item kedaluwarsa hanya menghilang dari antrian seolah-olah tidak pernah ditambahkan.
Rangkuman
Metode
Menambahkan item ke antrian.
Membaca satu atau lebih item dari antrian.
Menghapus item atau item yang sebelumnya dibaca dari antrian.
Properti
Metode
AddAsync
Menambahkan item ke antrian.
Parameter
Nilai item untuk ditambahkan ke antrian.
Waktu kedaluwarsa item, dalam detik, setelah itu item akan secara otomatis dihapus dari antrian.
Prioritas item. Item dengan prioritas tertinggi diambil dari antrian sebelum item dengan prioritas lebih rendah.
Memberikan nilai
ReadAsync
Membaca satu atau lebih item dari antrian sebagai operasi atom tunggal.
Metode ini tidak secara otomatis menghapus item yang dikembalikan dari antrian tetapi membuatnya tidak terlihat untuk panggilan ReadAsync lainnya selama periode penundaan tidak terlihat.Item harus dihapus secara eksplisit dari antrian dengan MemoryStoreQueue:RemoveAsync() sebelum batas waktu tidak terlihat berakhir.Waktu maksimal tidak terlihat default ke 30 detik kecuali nilai yang berbeda disediakan di MemoryStoreService:GetQueue() .
Parameter
Jumlah item untuk dilihat. Nilai maksimum yang diizinkan untuk parameter ini adalah 100.
Mengontrol perilaku metode dalam kasus antrian memiliki kurang dari count item: jika diatur ke false, metode mengembalikan semua item yang tersedia; jika diatur ke benar, metode tidak mengembalikan item.Nilai default adalah false.
Durasi, dalam detik, untuk mana metode akan menunggu jika nomor item yang diperlukan tidak tersedia segera di antrian.Membaca dicoba setiap dua detik selama periode ini.Parameter ini dapat ditetapkan menjadi nol untuk menunjukkan tidak ada tunggu.Jika parameter ini tidak disediakan atau diatur ke -1, metode akan menunggu tanpa batas waktu.
Memberikan nilai
Tuple dari dua elemen.Elemen pertama adalah array nilai item yang dibaca dari antrian.Elemen kedua adalah penanda string yang harus dikirim ke MemoryStoreQueue:RemoveAsync() untuk menghapus item-item ini dari antrian secara permanen.
Contoh Kode
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
Menghapus item atau item yang sebelumnya dibaca dari antrian.Metode ini menggunakan identifikator yang dikembalikan oleh MemoryStoreQueue:ReadAsync() untuk mengidentifikasi item yang harus dihapus.Jika dipanggil setelah batas waktu tidak terlihatnya habis, panggilan tidak berpengaruh.
Parameter
Mengidentifikasi item untuk dihapus. Gunakan nilai yang dikembalikan oleh MemoryStoreQueue:ReadAsync() .
Memberikan nilai
Contoh Kode
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