Klasa silnika
MemoryStoreHashMapPages
*Ta zawartość została przetłumaczona przy użyciu narzędzi AI (w wersji beta) i może zawierać błędy. Aby wyświetlić tę stronę w języku angielskim, kliknij tutaj.
Podsumowanie
Przykłady kodu
Wyświetlanie elementów w mapie skrótów 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("Ustawianie danych HashMap...")
local createdItems = {}
for index = 1, numItems do
local key = tostring(index) -- Klucze HashMap muszą być stringami
local value = `{key}_test_value`
local success, result = pcall(hashMap.SetAsync, hashMap, key, value, EXPIRATION)
if success then
createdItems[key] = value
else
warn(`Błąd ustawiania klucza {key}: {result}`)
end
end
print("Zakończono ustawianie danych HashMap.")
return createdItems
end
local function getItemsFromAllPages(pages: MemoryStoreHashMapPages): { [string]: any }
-- Czysto w celach logowania, śledzimy, na którym numerze strony jesteśmy
local currentPageNumber = 1
local retrievedItems = {}
while not pages.IsFinished do
print(`Pobieranie elementów na stronie {currentPageNumber}...`)
local items = pages:GetCurrentPage()
for _, entry in pairs(items) do
print(` {entry.key}: {entry.value}`)
retrievedItems[entry.key] = entry.value
end
-- Przechodzimy do następnej strony, jeśli są kolejne strony do przeczytania
if not pages.IsFinished then
pages:AdvanceToNextPageAsync()
currentPageNumber += 1
end
end
print("Zakończono czytanie wszystkich stron")
return retrievedItems
end
local function compareAllItems(retrievedItems: { [string]: any }, expectedItems: { [string]: any }): number
print("Porównywanie pobranych elementów z oczekiwanymi elementami...")
local numMatchingItems = 0
for key, expectedValue in pairs(expectedItems) do
if retrievedItems[key] == expectedValue then
numMatchingItems += 1
else
warn(`Niezgodna wartość pobrana dla klucza {key}: oczekiwano {expectedValue}, pobrano {retrievedItems[key]}`)
end
end
print("Porównanie zakończone!")
return numMatchingItems
end
-- Klucze dodane do hashmapy są także dodawane do tej tabeli expectedItems.
-- Później, pobrane elementy hashmapy będą porównywane z tą tabelą oczekiwanych elementów.
local expectedItems = populateHashMap(testHashMap, NUM_TEST_ITEMS)
-- Pobieranie stron może kończyć się błędem. W takim przypadku pozwolimy na błąd i zatrzymamy wykonanie programu,
-- ale możesz chciałbyś użyć pcall i obsłużyć to inaczej.
print(`Pobieranie stron HashMap z ListItemsAsync...`)
local pages = testHashMap:ListItemsAsync(NUM_TEST_ITEMS)
local retrievedItems = getItemsFromAllPages(pages)
local numMatchingItems = compareAllItems(retrievedItems, expectedItems)
-- Jeśli nie było błędów przy ustawianiu lub pobieraniu elementów, wszystkie elementy powinny pasować.
print(`Program zakończony. {numMatchingItems}/{NUM_TEST_ITEMS} pobranych elementów pasowało do oczekiwanych wartości.`)