MemoryStoreHashMap
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
ให้การเข้าถึงแผนที่ฮาชภายใน 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 รวม
พารามิเตอร์
จํานวนไอเทมสูงสุดที่สามารถส่งคืนได้
ส่งค่ากลับ
ตัวอย่าง MemoryStoreHashMapPages ที่ระบุรายการเป็น MemoryStoreHashMapPages ตัวอย่าง
ตัวอย่างโค้ด
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
ดึงค่าของกุญแจจากแผนที่คีย์และให้คุณอัปเดตเป็นค่าใหม่
วิธีนี้รับฟังก์ชัน callback ที่ดึงค่ากุญแจที่มีอยู่และส่งมันไปยังฟังก์ชันแปลงซึ่งส่งค่าใหม่สำหรับไอเทมด้วยข้อยกเว้นเหล่านี้:
- หากกุญแจไม่มีอยู่ ค่าเก่าที่ส่งไปยังฟังก์ชันคือ 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)