エンジンクラス
MemoryStoreHashMapPages
*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。
コードサンプル
メモリストアハッシュマップにアイテムをリスト表示する
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}_テスト値`
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
-- ハッシュマップに追加されたキーは、このexpectedItemsテーブルにも追加されます。
-- 後で、取得したハッシュマップアイテムはこの期待されるアイテムのテーブルと比較されます。
local expectedItems = populateHashMap(testHashMap, NUM_TEST_ITEMS)
-- ページを取得する際にエラーが発生することがあります。この場合、エラーのままプログラムの実行を停止しますが、
-- pcallを使用して異なる方法で処理することもできます。
print(`ListItemsAsyncを使用してHashMapページを取得しています...`)
local pages = testHashMap:ListItemsAsync(NUM_TEST_ITEMS)
local retrievedItems = getItemsFromAllPages(pages)
local numMatchingItems = compareAllItems(retrievedItems, expectedItems)
-- アイテムの設定または取得中にエラーが発生しなかった場合、すべてのアイテムは一致するはずです。
print(`プログラム完了。 {numMatchingItems}/{NUM_TEST_ITEMS} の取得アイテムが期待される値と一致しました。`)