MemoryStoreQueue
*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.
Cung cấp quyền truy cập vào hàng đợi trong MemoryStore.Một hàng đợi là một cấu trúc dữ liệu cung cấp kho lưu tạm thời cho các vật phẩm ngẫu nhiên (lên tới kích thước tối đa của vật phẩm -- xem Giới hạn MemoryStore ).Mỗi mục trong hàng đợi có ưu tiên số: MemoryStore trích xuất các mục có ưu tiên cao hơn từ hàng đợi trước tiên, và nó trích xuất các mục có ưu tiên tương tự theo thứ tự thêm.
Các mục trong hàng đợi có thể được đặt thành hết hạn sau một khoảng thời gian nhất định.Các mục hết hạn chỉ biến mất khỏi hàng đợi như thể chúng chưa bao giờ được thêm vào.
Tóm Tắt
Phương Pháp
Thêm một mục vào hàng đợi.
Đọc một hoặc nhiều mục từ hàng đợi.
Loại bỏ một vật phẩm hoặc các vật phẩm đã được đọc trước đó khỏi hàng chờ.
Thuộc Tính
Phương Pháp
AddAsync
Thêm một mục vào hàng đợi.
Tham Số
Giá trị của mục để thêm vào hàng đợi.
Thời gian hết hạn của mục, trong giây lát, sau đó mục sẽ tự động bị xóa khỏi hàng chờ.
Ưu tiên của mục. Các mục có ưu tiên cao hơn được lấy từ hàng đợi trước các mục có ưu tiên thấp hơn.
Lợi Nhuận
ReadAsync
Đọc một hoặc nhiều mục từ hàng đợi như một hoạt động nguyên tử duy nhất.
Phương pháp này không tự động xóa các vật phẩm trả lại từ hàng đợi nhưng làm cho chúng không hiển thị đối với các cuộc gọi ReadAsync khác trong thời gian hết hạn vô hình.Các mục phải được xóa rõ ràng khỏi hàng đợi với MemoryStoreQueue:RemoveAsync() trước khi thời gian chờ vô hiệu hóa hết hạn.Thời gian chờ vô hình mặc định là 30 giây trừ khi một giá trị khác được cung cấp trong MemoryStoreService:GetQueue() .
Tham Số
Số lượng vật phẩm để đã xem. Giá trị tối đa cho tham số này là 100.
Kiểm soát hành vi của phương thức trong trường hợp hàng đợi có ít hơn count vật phẩm: nếu đặt thành false, phương thức trả lại tất cả các vật phẩm có sẵn; nếu đặt thành true, nó không trả lại bất kỳ vật phẩm nào.Giá trị mặc định là false.
Thời lượng, bằng giây, mà phương thức sẽ chờ nếu số lượng vật phẩm cần thiết không có ngay lập tức trong hàng đợi.Các đọc được thử mỗi hai giây trong khoảng thời gian này.Tham số này có thể được đặt thành không để chỉ ra không có chờ đợi.Nếu tham số này không được cung cấp hoặc đặt thành -1, phương thức sẽ chờ vô cực.
Lợi Nhuận
Một tuple của hai yếu tố.Yếu tố đầu tiên là một mảng các giá trị mục được đọc từ hàng đợi.Yếu tố thứ hai là một nhận dạng chuỗi mà nên được chuyển đến MemoryStoreQueue:RemoveAsync() để vĩnh viễn xóa các mục này khỏi hàng đợi.
Mẫu mã
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
Loại bỏ một vật phẩm hoặc các vật phẩm đã được đọc trước đó khỏi hàng chờ.Phương pháp này sử dụng nhận dạng được trả về bởi MemoryStoreQueue:ReadAsync() để xác định các mục cần xóa.Nếu gọi sau khi hết thời gian chờ vô hiệu hóa, cuộc gọi không có hiệu lực.
Tham Số
Xác định các mục cần xóa. Sử dụng giá trị trả về bởi MemoryStoreQueue:ReadAsync() .
Lợi Nhuận
Mẫu mã
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