提供存取 MemoryStoreService 內的哈希樹。哈希樹是一個集合項目的資料,項目的鍵與隨機值聯絡(最大允許尺寸見 Memory Stores)。鍵沒有排序保證。
概要
方法
在擷取值的哈希地圖中取回鑰匙的值。
返回 MemoryStoreHashMapPages 對象以檢查掌握掃描樹狀圖中的項目。
從擷取地圖中移除一個項目。
設定擷取時鍵的值。
從擷取鑰匙值的哈希地圖中取回值,讓您可以更新它到新值。
屬性
方法
GetAsync
Variant
在擷取值的哈希地圖中取回鑰匙的值。
參數
你想要擷取的鑰匙的值。
返回
Variant
值,或為零,如果鑰匙不存在。
範例程式碼
Getting data from a MemoryStore Hash Map
local MemoryStoreService = game:GetService("MemoryStoreService")
local hashMap = MemoryStoreService:GetHashMap("HashMap1")
local key = "User_1234"
local value = 1000
local expiration = 30
local setSuccess, _ = pcall(function()
return hashMap:SetAsync(key, value, expiration)
end)
if setSuccess then
print("Set succeeded!")
end
local item
local getSuccess, getError = pcall(function()
item = hashMap:GetAsync(key)
end)
if getSuccess then
print(item)
else
warn(getError)
end
ListItemsAsync
返回 MemoryStoreHashMapPages 對象以索引瀚覽樹狀圖中的項目。範圍有效為 1 至 200 包括。
參數
可以返回的物品最大數量。
返回
一個 MemoryStoreHashMapPages 實例,用於列出項目為 MemoryStoreHashMapPages 個實例。
範例程式碼
Listing items in a MemoryStore Hash Map
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) -- HashMap keys must be strings
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 }
-- Purely for logging purposes, we track what page number we're on
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
-- Advance pages if there are more pages to read
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
-- Keys added to the hashmap are also added to this expectedItems table.
-- Later, the retrieved hashmap items will be compared against this table of expected items.
local expectedItems = populateHashMap(testHashMap, NUM_TEST_ITEMS)
-- Getting pages can error. In this case, we will let it error and stop program execution,
-- but you may want to pcall it and handle it differently.
print(`Getting HashMap pages with ListItemsAsync...`)
local pages = testHashMap:ListItemsAsync(NUM_TEST_ITEMS)
local retrievedItems = getItemsFromAllPages(pages)
local numMatchingItems = compareAllItems(retrievedItems, expectedItems)
-- If there were no errors setting or getting items, all items should match.
print(`Program complete. {numMatchingItems}/{NUM_TEST_ITEMS} retrieved items matched the expected values.`)
RemoveAsync
void
從擷取地圖中移除一個項目。
參數
移除的鑰匙。
返回
void
範例程式碼
Removing data from a MemoryStore Hash Map
local MemoryStoreService = game:GetService("MemoryStoreService")
local hashMap = MemoryStoreService:GetHashMap("HashMap1")
local key = "User_1234"
local value = 1000
local expiration = 30
local setSuccess, setError = pcall(function()
return hashMap:SetAsync(key, value, expiration)
end)
if not setSuccess then
warn(setError)
end
local removeSuccess, removeError = pcall(function()
hashMap:RemoveAsync(key)
end)
if removeSuccess then
print("Remove succeeded!")
else
warn(removeError)
end
SetAsync
在擷取地圖中的鑰匙值,覆蓋任何現有值。
參數
返回
範例程式碼
Adding data to a MemoryStore Hash Map
local MemoryStoreService = game:GetService("MemoryStoreService")
local hashMap = MemoryStoreService:GetHashMap("HashMap1")
local key = "User_1234"
local value = 1000
local expiration = 30
local setSuccess, setError = pcall(function()
return hashMap:SetAsync(key, value, expiration)
end)
if setSuccess then
print("Set succeeded!")
else
warn(setError)
end
UpdateAsync
Variant
從擷取鑰匙值的哈希地圖中取回值,讓您可以更新它到新值。
此方法接受一個回潮函數,它會擷取現有的鑰匙值並將其傳送給變形函數,以下是其中的一些例子:
- 如果鑰匙不存在,傳入功能的舊值為零。
- 如果函數返回 nil,則更新將取消。
新值只會在鑰匙未更新 (例如,由不同的遊戲服務器更新) 時儲已讀,以便在閱取時查看。如果值在此時變更,變形功能會再次呼叫,並且將最新的項目值傳回。此過程重複直到變形功能成功儲存為止,或者變形功能將零返回以取消運作。
參數
您想要更新的鑰匙。
您提供的變形函數。這個函數接受舊值作為輸入,並且返回新值。
項目在秒後自動從擷取地圖中移除。最大過期時間為 45 天 (3,888,000 秒)。
返回
Variant
變身函數的最後一個值。
範例程式碼
Updating a Memory Store Hash Map
local MemoryStoreService = game:GetService("MemoryStoreService")
local hashMap = MemoryStoreService:GetHashMap("ResourceInventory")
local function contributeResources(itemResource, addedCount)
local success, newResourceCount = pcall(function()
return hashMap:UpdateAsync(itemResource, function(resource)
resource = resource or {count = 0}
resource.count = resource.count + addedCount
-- ensure we don't exceed the maximum resource count
if resource.count > 500 then
resource.count = 500
end
return resource
end, 1200)
end)
if success then
print(newResourceCount)
end
end
contributeResources("myResource", 50)