MemoryStoreHashMap

แสดงที่เลิกใช้งานแล้ว

*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่

ไม่สามารถสร้าง
ไม่ซ้ำ

ให้การเข้าถึงแผนที่ฮาชภายใน MemoryStoreServiceแผนที่คีย์แฮชคือคอลเลกชันของไอเทมที่สตริงกุญแจเชื่อมโยงกับค่าสุ่ม (ถึงขนาดสูงสุดที่อนุญาต -- ดู คลังหน่วยความจำ )กุญแจไม่มีการรับประกันการจัดเรียง

สรุป

วิธีการ

  • GetAsync(key : string):Variant
    ผลตอบแทน

    ดึงค่าของกุญแจในแผนที่ฮาช

  • ผลตอบแทน

    คืนวัตถุ MemoryStoreHashMapPages สำหรับการจัดเรียงผ่านรายการในแผนที่แฮช

  • ผลตอบแทน

    ลบรายการออกจากแผนที่ความเร็ว

  • SetAsync(key : string,value : Variant,expiration : number):boolean
    ผลตอบแทน

    ตั้งค่าค่าของกุญแจในแผนที่ฮาช

  • UpdateAsync(key : string,transformFunction : function,expiration : number):Variant
    ผลตอบแทน

    ดึงค่าของกุญแจจากแผนที่คีย์และให้คุณอัปเดตเป็นค่าใหม่

คุณสมบัติ

วิธีการ

GetAsync

Variant
ผลตอบแทน

ดึงค่าของกุญแจในแผนที่ฮาช

พารามิเตอร์

key: string

ดึงข้อมูล

ค่าเริ่มต้น: ""

ส่งค่ากลับ

Variant

ค่าหรือ nil หากกุญแจไม่มีอยู่

ตัวอย่างโค้ด

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 ตัวอย่าง

ตัวอย่างโค้ด

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

()
ผลตอบแทน

ลบรายการออกจากแผนที่ความเร็ว

พารามิเตอร์

key: string

กุญแจในการลบ

ค่าเริ่มต้น: ""

ส่งค่ากลับ

()

ตัวอย่างโค้ด

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
ผลตอบแทน

ดึงค่าของกุญแจจากแผนที่คีย์และให้คุณอัปเดตเป็นค่าใหม่

วิธีนี้รับฟังก์ชัน callback ที่ดึงค่ากุญแจที่มีอยู่และส่งมันไปยังฟังก์ชันแปลงซึ่งส่งค่าใหม่สำหรับไอเทมด้วยข้อยกเว้นเหล่านี้:

  • หากกุญแจไม่มีอยู่ ค่าเก่าที่ส่งไปยังฟังก์ชันคือ nil
  • หากฟังก์ชัน返回值 nil การอัปเดตจะถูกยกเลิก

ค่าใหม่จะถูกบันทึกเฉพาะถ้ากุญแจไม่ได้ถูกอัปเดต (ตัวอย่างเช่นโดยเซิร์ฟเวอร์เกมที่แตกต่างกัน) ตั้งแต่ช่วงเวลาที่อ่านได้หากมีการเปลี่ยนแปลงค่าในเวลานั้น ฟังก์ชันแปลงจะถูกเรียกอีกครั้งด้วยค่ารายการล่าสุดวงจรนี้จะทำซ้ำจนกว่าค่าจะถูกบันทึกอย่างประสบความสำเร็จหรือฟังก์ชันการแปลงจะส่งคืน nil เพื่อยกเลิกการดำเนินการ

พารามิเตอร์

key: string

กุญแจที่มีค่าที่คุณต้องการอัปเดต

ค่าเริ่มต้น: ""
transformFunction: function

ฟังก์ชันแปลงที่คุณให้ ฟังก์ชันนี้รับค่าเก่าเป็นอินพุตและส่งค่าใหม่กลับ

ค่าเริ่มต้น: ""
expiration: number

รายการหมดอายุภายในไม่กี่วินาทีหลังจากนั้นรายการจะถูกลบออกจากแผนที่แฮชโดยอัตโนมัติเวลาหมดอายุสูงสุดคือ 45 วัน (3,888,000 วินาที)

ค่าเริ่มต้น: ""

ส่งค่ากลับ

Variant

ค่าสุดท้ายที่ส่งคืนโดยฟังก์ชันแปลง

ตัวอย่างโค้ด

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)

อีเวนต์