Engine Class
MemoryStoreHashMapPages
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
สรุป
ตัวอย่างโค้ด
รายการรายการในแผนที่แฮชของ 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("กำลังตั้งค่าข้อมูล HashMap...")
local createdItems = {}
for index = 1, numItems do
local key = tostring(index) -- คีย์ HashMap จะต้องเป็นสตริง
local value = `{key}_test_value`
local success, result = pcall(hashMap.SetAsync, hashMap, key, value, EXPIRATION)
if success then
createdItems[key] = value
else
warn(`เกิดข้อผิดพลาดในการตั้งค่าคีย์ {key}: {result}`)
end
end
print("ตั้งค่าข้อมูล HashMap เสร็จสิ้น.")
return createdItems
end
local function getItemsFromAllPages(pages: MemoryStoreHashMapPages): { [string]: any }
-- เนื่องจากเป็นเพียงเพื่อการบันทึกข้อมูล เราจึงติดตามหมายเลขหน้าที่เราอยู่
local currentPageNumber = 1
local retrievedItems = {}
while not pages.IsFinished do
print(`กำลังดึงข้อมูลในหน้า {currentPageNumber}...`)
local items = pages:GetCurrentPage()
for _, entry in pairs(items) do
print(` {entry.key}: {entry.value}`)
retrievedItems[entry.key] = entry.value
end
-- เพิ่มหน้าย้อนกลับหากมีหน้ามากขึ้นให้อ่าน
if not pages.IsFinished then
pages:AdvanceToNextPageAsync()
currentPageNumber += 1
end
end
print("เสร็จสิ้นการอ่านทุกหน้า")
return retrievedItems
end
local function compareAllItems(retrievedItems: { [string]: any }, expectedItems: { [string]: any }): number
print("กำลังเปรียบเทียบรายการที่ดึงมากับรายการที่คาดหวัง...")
local numMatchingItems = 0
for key, expectedValue in pairs(expectedItems) do
if retrievedItems[key] == expectedValue then
numMatchingItems += 1
else
warn(`ค่าที่ดึงมาจากคีย์ {key} ไม่ตรงกัน: คาดว่าจะเป็น {expectedValue}, ดึงมาเป็น {retrievedItems[key]}`)
end
end
print("การเปรียบเทียบเสร็จสิ้น!")
return numMatchingItems
end
-- คีย์ที่เพิ่มใน hashmap จะถูกเพิ่มไปยังตาราง expectedItems นี้ด้วย
-- ต่อมา รายการ hashmap ที่ดึงมาจะถูกเปรียบเทียบกับตารางที่คาดหวังนี้
local expectedItems = populateHashMap(testHashMap, NUM_TEST_ITEMS)
-- การดึงหน้าสามารถมีข้อผิดพลาด ในกรณีนี้ เราจะปล่อยให้เกิดข้อผิดพลาดและหยุดการทำงานของโปรแกรม
-- แต่คุณอาจต้อง pcall และจัดการกับมันแตกต่างออกไป
print(`กำลังดึงหน้า HashMap ด้วย ListItemsAsync...`)
local pages = testHashMap:ListItemsAsync(NUM_TEST_ITEMS)
local retrievedItems = getItemsFromAllPages(pages)
local numMatchingItems = compareAllItems(retrievedItems, expectedItems)
-- หากไม่มีข้อผิดพลาดในการตั้งค่าหรือดึงข้อมูล ทุกอย่างจะต้องตรงกัน
print(`โปรแกรมเสร็จสิ้น. {numMatchingItems}/{NUM_TEST_ITEMS} รายการที่ดึงมา match กับค่าที่คาดหวัง.`)