Lớp công cụ
MemoryStoreHashMap
*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.
Tóm Tắt
Phương Pháp
RemoveAsync(key: string):() |
UpdateAsync(key: string,transformFunction: function,expiration: number):Variant |
Tài Liệu Tham Khảo API
Phương Pháp
GetAsync
Tham Số
Lợi Nhuận
Variant
Mẫu mã
Lấy dữ liệu từ một MemoryStore Hash Map
local MemoryStoreService = game:GetService("MemoryStoreService")
local hashMap = MemoryStoreService:GetHashMap("HashMap1")
local key = "User_1234"
local value = 1000
local expiration = 30
local setSuccess, _ = pcall(function()
return hashMap:SetAsync(key, value, expiration)
end)
if setSuccess then
print("Đặt thành công!")
end
local item
local getSuccess, getError = pcall(function()
item = hashMap:GetAsync(key)
end)
if getSuccess then
print(item)
else
warn(getError)
endListItemsAsync
Tham Số
Lợi Nhuận
Mẫu mã
Liệt kê các mục trong Hash Map MemoryStore
local MemoryStoreService = game:GetService("MemoryStoreService")
local testHashMap = MemoryStoreService:GetHashMap("HashMap1")
local EXPIRATION = 600
local NUM_TEST_ITEMS = 32
local function populateHashMap(hashMap: MemoryStoreHashMap, numItems: number): { [string]: any }
print("Đang thiết lập dữ liệu HashMap...")
local createdItems = {}
for index = 1, numItems do
local key = tostring(index) -- Các khóa HashMap phải là chuỗi
local value = `{key}_test_value`
local success, result = pcall(hashMap.SetAsync, hashMap, key, value, EXPIRATION)
if success then
createdItems[key] = value
else
warn(`Lỗi khi thiết lập khóa {key}: {result}`)
end
end
print("Đã hoàn thành việc thiết lập dữ liệu HashMap.")
return createdItems
end
local function getItemsFromAllPages(pages: MemoryStoreHashMapPages): { [string]: any }
-- Chỉ để mục đích ghi log, chúng tôi theo dõi số trang mà chúng tôi đang ở
local currentPageNumber = 1
local retrievedItems = {}
while not pages.IsFinished do
print(`Đang lấy các mục trên trang {currentPageNumber}...`)
local items = pages:GetCurrentPage()
for _, entry in pairs(items) do
print(` {entry.key}: {entry.value}`)
retrievedItems[entry.key] = entry.value
end
-- Tiến đến trang tiếp theo nếu còn trang để đọc
if not pages.IsFinished then
pages:AdvanceToNextPageAsync()
currentPageNumber += 1
end
end
print("Đã hoàn thành việc đọc tất cả các trang")
return retrievedItems
end
local function compareAllItems(retrievedItems: { [string]: any }, expectedItems: { [string]: any }): number
print("So sánh các mục đã lấy với các mục dự kiến...")
local numMatchingItems = 0
for key, expectedValue in pairs(expectedItems) do
if retrievedItems[key] == expectedValue then
numMatchingItems += 1
else
warn(`Giá trị đã lấy không khớp cho khóa {key}: dự kiến {expectedValue}, đã lấy {retrievedItems[key]}`)
end
end
print("Hoàn thành so sánh!")
return numMatchingItems
end
-- Các khóa được thêm vào hashmap cũng được thêm vào bảng expectedItems này.
-- Sau đó, các mục hashmap đã lấy sẽ được so sánh với bảng các mục dự kiến này.
local expectedItems = populateHashMap(testHashMap, NUM_TEST_ITEMS)
-- Việc lấy các trang có thể gặp lỗi. Trong trường hợp này, chúng tôi sẽ để nó lỗi và dừng thực thi chương trình,
-- nhưng bạn có thể muốn pcall nó và xử lý khác đi.
print(`Đang lấy các trang HashMap với ListItemsAsync...`)
local pages = testHashMap:ListItemsAsync(NUM_TEST_ITEMS)
local retrievedItems = getItemsFromAllPages(pages)
local numMatchingItems = compareAllItems(retrievedItems, expectedItems)
-- Nếu không có lỗi trong việc thiết lập hoặc lấy các mục, tất cả các mục nên khớp.
print(`Chương trình hoàn tất. {numMatchingItems}/{NUM_TEST_ITEMS} các mục đã lấy khớp với các giá trị dự kiến.`)RemoveAsync
Tham Số
Lợi Nhuận
()
Mẫu mã
Xóa dữ liệu từ Bản đồ Bộ nhớ
local MemoryStoreService = game:GetService("MemoryStoreService")
local hashMap = MemoryStoreService:GetHashMap("HashMap1")
local key = "User_1234"
local value = 1000
local expiration = 30
local setSuccess, setError = pcall(function()
return hashMap:SetAsync(key, value, expiration)
end)
if not setSuccess then
warn(setError)
end
local removeSuccess, removeError = pcall(function()
hashMap:RemoveAsync(key)
end)
if removeSuccess then
print("Xóa thành công!")
else
warn(removeError)
endSetAsync
Lợi Nhuận
Mẫu mã
Thêm dữ liệu vào Bản đồ Hash của MemoryStore
local MemoryStoreService = game:GetService("MemoryStoreService")
local hashMap = MemoryStoreService:GetHashMap("HashMap1")
local key = "User_1234"
local value = 1000
local expiration = 30
local setSuccess, setError = pcall(function()
return hashMap:SetAsync(key, value, expiration)
end)
if setSuccess then
print("Đặt thành công!")
else
warn(setError)
endUpdateAsync
Lợi Nhuận
Variant
Mẫu mã
Cập nhật một Bảng Băm Bộ Nhớ
local MemoryStoreService = game:GetService("MemoryStoreService")
local hashMap = MemoryStoreService:GetHashMap("ResourceInventory")
local function contributeResources(itemResource, addedCount)
local success, newResourceCount = pcall(function()
return hashMap:UpdateAsync(itemResource, function(resource)
resource = resource or { count = 0 }
resource.count = resource.count + addedCount
-- đảm bảo rằng chúng ta không vượt quá số lượng tài nguyên tối đa
if resource.count > 500 then
resource.count = 500
end
return resource
end, 1200)
end)
if success then
print(newResourceCount)
end
end
contributeResources("myResource", 50)