MemoryStoreHashMap

Hiển Thị Bản Đã Lỗi Thời

*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.

Không Thể Tạo
Không Sao Chép

Cung cấp quyền truy cập vào bản đồ hash trong MemoryStoreService .Bản đồ hash là một bộ sưu tập các vật phẩm mà chìa khóa chuỗi được liên kết với các giá trị ngẫu nhiên (lên tới kích thước tối đa cho phép -- xem Kho lưu trữ bộ nhớ ).Các chìa khóa không có bảo đảm xếp hạng.

Tóm Tắt

Phương Pháp

Thuộc Tính

Phương Pháp

GetAsync

Variant
Sinh Lợi

Lấy giá trị của một chìa khóa trong bản đồ hash.

Tham Số

key: string

Chìa khóa có giá trị mà bạn muốn lấy lại.

Giá Trị Mặc Định: ""

Lợi Nhuận

Variant

Giá trị, hoặc nil nếu chìa khóa không tồn tại.

Mẫu mã

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
Sinh Lợi

Trả về một đối tượng MemoryStoreHashMapPages để liệt kê qua các mục trong bản đồ hash. Phạm vi hợp lệ là 1 đến 200 bao gồm.

Tham Số

count: number

Số lượng tối đa các mục có thể được trả lại.

Giá Trị Mặc Định: ""

Lợi Nhuận

Một ví dụ MemoryStoreHashMapPages mà liệt kê các mục như MemoryStoreHashMapPages các ví dụ.

Mẫu mã

The code below lists all the items in a Memory Store Hash Map.

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

()
Sinh Lợi

Loại bỏ một vật phẩm khỏi bản đồ hash.

Tham Số

key: string

Chìa khóa để xóa.

Giá Trị Mặc Định: ""

Lợi Nhuận

()

Mẫu mã

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

Sinh Lợi

Đặt giá trị của một chìa khóa trong bản đồ hash, ghi đè bất kỳ giá trị hiện có nào.

Tham Số

key: string

Chìa khóa có giá trị để cài đặt.

Giá Trị Mặc Định: ""
value: Variant

Giá trị để cài đặt.

Giá Trị Mặc Định: ""
expiration: number

Vật phẩm hết hạn trong vài giây, sau đó vật phẩm sẽ tự động bị xóa khỏi bản đồ hash.Thời gian hết hạn tối đa là 45 ngày (3,888,000 giây).

Giá Trị Mặc Định: ""

Lợi Nhuận

Mẫu mã

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
Sinh Lợi

Lấy giá trị của một chìa khóa từ bản đồ hash và cho phép bạn cập nhật nó lên giá trị mới.

Phương pháp này chấp nhận một chức năng gọi lại lấy giá trị chìa khóa hiện tại và chuyển nó cho một chức năng biến đổi, trả lại giá trị mới cho vật phẩm, với những ngoại lệ sau:

  • Nếu chìa khóa không tồn tại, giá trị cũ được chuyển cho chức năng là nil .
  • Nếu chức năng trả về nil , cập nhật sẽ bị hủy.

Giá trị mới chỉ được lưu nếu chìa khóa không được cập nhật ( ví dụ, bởi một máy chủ trò chơi khác) kể từ khi nó được đã xem.Nếu giá trị thay đổi trong thời gian đó, chức năng biến đổi được gọi lại với giá trị mục gần đây nhất.Vòng lặp này lặp lại cho đến khi giá trị được lưu thành công hoặc chức năng biến đổi trả về nil để hủy bỏ hoạt động.

Tham Số

key: string

Chìa khóa có giá trị mà bạn muốn cập nhật.

Giá Trị Mặc Định: ""
transformFunction: function

Chức năng biến đổi, mà bạn cung cấp. Chức năng này nhận giá trị cũ làm đầu vào và trả giá trị mới.

Giá Trị Mặc Định: ""
expiration: number

Vật phẩm hết hạn trong vài giây, sau đó vật phẩm sẽ tự động bị xóa khỏi bản đồ hash.Thời gian hết hạn tối đa là 45 ngày (3,888,000 giây).

Giá Trị Mặc Định: ""

Lợi Nhuận

Variant

Giá trị cuối cùng được trả bởi chức năng biến đổi.

Mẫu mã

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.

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)

Sự Kiện