提供在 MemoryStoreService 内访问哈希地图的访问。哈希地图是一个集合中的项目,其中字符串键与随机值关联(最大允许尺寸为内存存储)。钥匙没有排序保证。
概要
方法
从哈希地图中检索键的值。
返回一个 MemoryStoreHashMapPages 对象,用于通过哈希地图中的项目进行枚列。
从哈希地图中删除一个项目。
设置哈希地图中键值。
从哈希地图中检索钥匙的值,并允许您将其更新为新值。
属性
方法
GetAsync
从哈希地图中检索键的值。
参数
你想要取回的值为的钥匙。
返回
值或nil。
代码示例
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 包括在内。
参数
最大可返回的物品数量。
返回
代码示例
The code below lists all the items in a Memory Store 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
从哈希地图中删除一个项目。
参数
要移除除的关键。
返回
代码示例
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
在哈希地图中设置键的值,覆盖任何现有值。
参数
要设置的值的钥匙。
要设置的值。
物品在秒内过期,过期后物品将自动从哈希地图中删除。最大有效期为 45 天(3,888,000 秒)。
返回
代码示例
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
从哈希地图中检索钥匙的值,并允许您将其更新为新值。
该方法接受一个回调函数,该函数检索现有的键值并将其传递给转换函数,该函数返回物品目的新值,但以下情况除外:
- 如果键不存在,传给函数的旧值是 nil .
- 如果函数返回 nil,更新将被取消。
新值只有在键未更新(例如,由不同的游戏服务器)从阅已读时刻起,才会被保存。如果在那段时间内值发生了变化,变形函数将再次调用最新的项目值。这个循环会继续重复,直到值得成功保存或变换函数返回 nil 以终止操作。
参数
值为您想要更新的钥匙。
你提供的转换函数。该函数将旧值作为输入,返回新值。
物品在秒内过期,过期后物品将自动从哈希地图中删除。最大有效期为 45 天(3,888,000 秒)。
返回
变换函数返回的最后值。
代码示例
This sample updates the count of some resource in a shared inventory. The use of MemoryStoreHashMap:UpdateAsync() ensures that all player contributions make their way into this shared inventory, even if the contributions occur simultaneously. The function enforces a max resource count of 500.
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)