MemoryStoreHashMap

显示已弃用

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

无法创建
未复制

提供存取 MemoryStoreService 内的哈希地图。哈希地图是一个包含随机值 (最大允许大小 - 请参阅 存储器 ) 的收藏。钥匙没有排序保证。

概要

方法

属性

方法

GetAsync

Variant
暂停

检索哈希地图中的钥匙值。

参数

key: string

您想要取回的钥匙的值。


返回

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
暂停

返回一个 MemoryStoreHashMapPages 对象,用于在哈希地图上列出物品。有效范围为 1 到 200 包括。

参数

count: number

可以返回的最大可能数量的物品。


返回

一个 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
暂停

从哈希地图中移除一个项目。

参数

key: string

删移除的钥匙。


返回

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

暂停

将键的值设置在哈希地图中,覆盖现有值。

参数

key: string

设置的钥匙。

value: Variant

设置值。

expiration: number

物品在秒后过期,过期后,物品将自动从哈希地图中删除。最大过期时间为 45 天 (3,888,000 秒)。


返回

代码示例

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,更新将被取消。

值只有在钥匙未更新(例如,由其他游戏服务器更已读)之后保存,从那一刻起就不会保存新值(例如,通过更新其他游戏服务器)。如果值在那一时间变更,变形函数将调用最新的物品值。此过程将重复直到值成功保存为止或变形函数返回 nil 来中止操作。

参数

key: string

您想要更新的钥匙。

transformFunction: function

您提供的变形函数。这个函数将旧值作为输入,并将新值返回。

expiration: number

物品在秒后过期,过期后,物品将自动从哈希地图中删除。最大过期时间为 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)

活动