MemoryStoreHashMapPages
แสดงที่เลิกใช้งานแล้ว
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
ประเภทพิเศษของวัตถุ Pages ที่หน้ามีคู่คีย์-ค่าจาก MemoryStoreHashMap .Pages:GetCurrentPage() สามารถใช้เพื่อดึงรายการของตารางแต่ละรายการที่มีกุญแจและค่า; สิ่งเหล่านี้สะท้อนข้อมูลคู่คีย์-ค่า
ตัวอย่างโค้ด
รหัสด้านล่างระบุรายการทั้งหมดในแผนที่แฮชคลังหน่วยความจํา
รายการรายการในแผนที่แฮชของ 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("Setting HashMap data...")
local createdItems = {}
for index = 1, numItems do
local key = tostring(index) -- คีย์แผนที่ต้องเป็นข้อความ
local value = `{key}_test_value`
local success, result = pcall(hashMap.SetAsync, hashMap, key, value, EXPIRATION)
if success then
createdItems[key] = value
else
warn(`Error setting key {key}: {result}`)
end
end
print("Done setting HashMap data.")
return createdItems
end
local function getItemsFromAllPages(pages: MemoryStoreHashMapPages): { [string]: any }
-- เพื่อจุดประสงค์ในการบันทึกเท่านั้น เราติดตามหมายเลขหน้าที่เราอยู่
local currentPageNumber = 1
local retrievedItems = {}
while not pages.IsFinished do
print(`Getting items on page {currentPageNumber}...`)
local items = pages:GetCurrentPage()
for _, entry in pairs(items) do
print(`\t{entry.key}: {entry.value}`)
retrievedItems[entry.key] = entry.value
end
-- หน้าล่วงหน้าหากมีหน้าเพิ่มเติมที่จะอ่าน
if not pages.IsFinished then
pages:AdvanceToNextPageAsync()
currentPageNumber += 1
end
end
print("Finished reading all pages")
return retrievedItems
end
local function compareAllItems(retrievedItems: { [string]: any }, expectedItems: { [string]: any }): number
print("Comparing retrieved items to expected items...")
local numMatchingItems = 0
for key, expectedValue in pairs(expectedItems) do
if retrievedItems[key] == expectedValue then
numMatchingItems += 1
else
warn(`Mismatched retrieved value for key {key}: expected {expectedValue}, retrieved {retrievedItems[key]}`)
end
end
print("Comparison complete!")
return numMatchingItems
end
-- กุญแจที่เพิ่มลงในแผนที่คีย์ยังถูกเพิ่มลงในตาราง expectedItems นี้
-- ในภายหลังรายการแผนที่ความสัมพันธ์ที่ดึงมาจะถูกเปรียบเทียบกับตารางรายการที่คาดว่า
local expectedItems = populateHashMap(testHashMap, NUM_TEST_ITEMS)
-- การรับหน้าอาจผิดพลาด ในกรณีนี้เราจะปล่อยให้มันผิดพลาดและหยุดการดำเนินการโปรแกรม
-- แต่คุณอาจต้องการโทรออกและจัดการมันอย่างแตกต่าง
print(`Getting HashMap pages with ListItemsAsync...`)
local pages = testHashMap:ListItemsAsync(NUM_TEST_ITEMS)
local retrievedItems = getItemsFromAllPages(pages)
local numMatchingItems = compareAllItems(retrievedItems, expectedItems)
-- หากไม่มีการตั้งค่าข้อผิดพลาดหรือรับไอเทม ไอเทมทั้งหมดควรตรงกัน
print(`Program complete. {numMatchingItems}/{NUM_TEST_ITEMS} retrieved items matched the expected values.`)
สรุป
วิธีการ
วิธีการรับทอดมาจากPagesคืนรายการบนหน้าปัจจุบัน คีย์ในรายการจะถูกกำหนดโดยแหล่งของวัตถุนี้
จัดเตรียมการไปยังหน้าต่อไปในวัตถุหน้าหากเป็นไปได้
คุณสมบัติ
คุณสมบัติ รับทอดมาจาก Pages
วิธีการ
คุณสมบัติ รับทอดมาจาก Pages