エンジンクラス
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("ハッシュマップデータを設定しています...")
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(`キー {key} の設定中にエラーが発生しました: {result}`)
end
end
print("ハッシュマップデータの設定が完了しました。")
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 でハッシュマップページを取得しています...`)
local pages = testHashMap:ListItemsAsync(NUM_TEST_ITEMS)
local retrievedItems = getItemsFromAllPages(pages)
local numMatchingItems = compareAllItems(retrievedItems, expectedItems)
-- アイテムの設定や取得中にエラーがなければ、すべてのアイテムが一致するはずです。
print(`プログラムが完了しました。{numMatchingItems}/{NUM_TEST_ITEMS} の取得したアイテムが期待される値と一致しました。`)