메모리 저장소 정렬된 맵

*이 콘텐츠는 AI(베타)를 사용해 번역되었으며, 오류가 있을 수 있습니다. 이 페이지를 영어로 보려면 여기를 클릭하세요.

정렬된 맵 데이터 구조는 메모리 저장소에서 자주 사용하는 인메모리 데이터를 선택적 정렬 키와 함께 키-값 쌍으로 저장하고, 정렬 키와 키에 따라 특정 순서를 유지할 수 있게 해줍니다. 큐와는 달리, 맵에 들어가는 키의 순서는 처리 순서를 결정하지 않으므로, 정렬된 맵은 게임 내 단체의 참여를 위한 정렬 기반 데이터 조직을 구현하는 데 유용합니다. 예를 들어, 리더보드와 서버 간 경매에 사용될 수 있습니다.

한계

데이터 구조 크기 한계 외에도, 정렬된 맵은 128자 키 크기 제한, 32KB 값 크기 제한 및 128자 정렬 키 크기 제한이 있습니다.

게임에서 이 한계를 초과하는 데이터를 저장해야 하는 경우, 키 접두사를 통해 여러 데이터 구조로 분할하고 분산시키는 샤딩 기법을 채택할 수 있습니다. 메모리 저장소를 샤딩하면 시스템의 확장성을 개선하는 데 도움이 될 수 있습니다.

정렬된 맵 가져오기

정렬된 맵을 가져오려면 MemoryStoreService:GetSortedMap()을 호출하고 맵에 정의하려는 이름을 전달합니다. 이름은 게임 내에서 전역적이므로, 이름을 사용하여 어떤 스크립트에서든 같은 정렬된 맵에 접근할 수 있습니다.

정렬된 맵 가져오기

local MemoryStoreService = game:GetService("MemoryStoreService")
local sortedMap = MemoryStoreService:GetSortedMap("SortedMap1")

정렬된 맵을 가져온 후, 다음 함수를 호출할 수 있습니다:

함수작업
MemoryStoreSortedMap:SetAsync()데이터 추가 또는 기존 키의 값 및/또는 정렬 키를 덮어씁니다.
MemoryStoreSortedMap:GetAsync()특정 키 읽기.
MemoryStoreSortedMap:GetRangeAsync()모든 기존 키 읽기 또는 특정 범위를 읽습니다.
MemoryStoreSortedMap:UpdateAsync()값 업데이트 후 정렬된 맵에서 키와/또는 정렬 키를 가져옵니다.
MemoryStoreSortedMap:RemoveAsync()키 제거 정렬된 맵에서.
MemoryStoreSortedMap:GetSizeAsync()크기 얻기 정렬된 맵의 항목 수.

데이터 추가 또는 덮어쓰기

정렬된 맵에서 새 키를 추가하거나 키의 값 또는 정렬 키를 덮어쓰려면 MemoryStoreSortedMap:SetAsync()을 호출하고 키 이름, , 초 단위의 만료 시간선택적 정렬 키를 전달합니다. 키가 만료되면 메모리가 자동으로 정리됩니다. 최대 만료 시간은 3,888,000초(45일)입니다. 제공된 경우 정렬 키는 유효한 숫자(정수 또는 부동 소수점) 또는 문자열이어야 합니다.

키의 정렬 순서에서 정렬 키가 키보다 우선합니다. 예를 들어, 오름차순으로 정렬할 때, 숫자 정렬 키가 먼저 정렬되고, 그 다음에는 문자열 정렬 키가 정렬되며, 정렬 키가 없는 항목이 나중에 정렬됩니다. 숫자 정렬 키가 있는 모든 항목은 정렬 키로 정렬되고, 두 항목의 정렬 키가 같으면 키로 정렬됩니다. 마찬가지로 문자열 정렬 키가 있는 모든 항목도 정렬 키로 정렬되고, 두 항목의 정렬 키가 같으면 키로 정렬됩니다. 정렬 키가 없는 모든 항목은 키로만 정렬됩니다.

오름차순으로 정렬된 데이터의 예 -


{key: "player1", value: someValue1, sortKey: -1}
{key: "player2", value: someValue2, sortKey: 0}
{key: "player4", value: someValue3, sortKey: 1}
{key: "player5", value: someValue4, sortKey: 1}
{key: "player3", value: someValue5, sortKey: 3.14}
{key: "player6", value: someValue6, sortKey: "someString"}
{key: "player0", value: someValue7}
{key: "player7", value: someValue8}

player0이 정렬 키가 있는 모든 키 뒤에 정렬되는 것을 주목하십시오. player6은 숫자 정렬 키가 있는 모든 키 뒤에 정렬됩니다. player4player5는 동일한 정렬 키를 가지고 있으므로 키에 따라 오름차순으로 정렬됩니다.

정렬된 맵에 데이터 추가

local MemoryStoreService = game:GetService("MemoryStoreService")
local sortedMap = MemoryStoreService:GetSortedMap("SortedMap1")
local setSuccess, _ = pcall(function()
return sortedMap:SetAsync("User_1234", 1000, 30, 3.14152)
end)
if setSuccess then
print("Set succeeded.")
end

데이터 가져오기

특정 키와 연결된 데이터 값 및 정렬 키를 가져오거나 범위 내의 여러 키에 대한 값과 정렬 키를 가져올 수 있습니다.

하나의 키로 데이터 가져오기

정렬된 맵에서 하나의 키와 연결된 값 및 정렬 키를 가져오려면, 키 이름과 함께 MemoryStoreSortedMap:GetAsync()을 호출합니다.

정렬된 맵에서 특정 키 가져오기

local MemoryStoreService = game:GetService("MemoryStoreService")
local sortedMap = MemoryStoreService:GetSortedMap("SortedMap1")
local setSuccess, _ = pcall(function()
return sortedMap:SetAsync("User_1234", 1000, 30, 3.14152)
end)
if setSuccess then
print("Set succeeded.")
end
local item
local getSuccess, getError = pcall(function()
item = sortedMap:GetAsync("User_1234")
end)
if getSuccess then
print(item)
else
warn(getError)
end

여러 키로 데이터 가져오기

정렬된 맵에서 여러 키에 대한 데이터를 단일 작업으로 가져오려면 MemoryStoreSortedMap:GetRangeAsync()을 호출합니다. 이 함수는 기본적으로 모든 기존 키를 나열하지만, 키 범위의 상한과 하한을 설정할 수 있습니다. 예를 들어, 다음 코드 샘플은 정렬된 맵의 시작부터 최대 20항목을 가져오며, 키는 10 이상이고 정렬 키는 100 이상이며 키는 50 이하, 정렬 키는 500 이하인 항목을 검색합니다.

정렬된 맵에서 키 범위 가져오기

local MemoryStoreService = game:GetService("MemoryStoreService")
local sortedMap = MemoryStoreService:GetSortedMap("SortedMap1")
local lowerBound = {}
lowerBound["key"] = "10"
lowerBound["sortKey"] = 100
local upperBound = {}
upperBound["key"] = "50"
upperBound["sortKey"] = 500
-- 시작 지점에서 최대 20항목 가져오기
local getSuccess, items = pcall(function()
return sortedMap:GetRangeAsync(
Enum.SortDirection.Ascending, 20, lowerBound, upperBound)
end)
if getSuccess then
for _, item in items do
print(item.key)
print(item.sortKey)
end
end

데이터 업데이트

정렬된 맵에서 키의 값과 정렬 키를 검색하여 업데이트하려면 MemoryStoreSortedMap:UpdateAsync()을 호출하고 키 이름, 값을 업데이트할 콜백 함수, 및 초 단위의 만료 시간을 전달합니다. 최대 만료 시간은 3,888,000초(45일)입니다.

대부분의 게임에서는 여러 서버가 동시에 동일한 키를 업데이트하고 값을 변경할 수 있습니다. MemoryStoreSortedMap:UpdateAsync()는 업데이트하기 전에 항상 최신 값을 수정하기 때문에, 이를 사용하여 콜백 함수의 입력으로 최신 값을 읽고 사용해야 합니다.

예를 들어, 다음 코드 샘플은 리더보드에서 플레이어의 점수를 업데이트합니다. 점수는 처치 수 / 사망 수로 계산됩니다. MemoryStoreSortedMap:UpdateAsync()는 여러 게임 서버가 같은 항목을 동시에 업데이트하더라도 처치 수와 사망 수가 최신 값으로 업데이트되도록 보장합니다. 플레이어의 처치 수와 사망 수는 단조롭게 증가하는 값이며 세션 내에서만 증가할 수 있습니다.

정렬된 맵에서 플레이어의 리더보드 점수 업데이트

local MemoryStoreService = game:GetService("MemoryStoreService")
local sortedMap = MemoryStoreService:GetSortedMap("Leaderboard")
local function updateLeaderboard(itemKey, killsToAdd, deathsToAdd)
local success, newStats, newScore = pcall(function()
return sortedMap:UpdateAsync(itemKey, function(playerStats, playerScore)
playerStats = playerStats or { kills = 0, deaths = 0 }
playerStats.kills += killsToAdd
playerStats.deaths += deathsToAdd
if playerStats then
-- `playerScore`는 맵에서 항목을 정렬하는 데 사용되는 sortKey입니다.
playerScore = playerStats.kills / math.max(playerStats.deaths, 1)
return playerStats, playerScore
end
return nil
end, 30)
end)
if success then
print(newStats)
print(newScore)
end
end

MemoryStoreSortedMap:UpdateAsync()의 대기 시간은 MemoryStoreSortedMap:GetAsync()MemoryStoreSortedMap:SetAsync()와 유사하지만, 경합이 발생할 경우에는 다를 수 있습니다.

경합이 발생하면, 시스템은 자동으로 작업을 재시도하며 다음 중 하나가 발생할 때까지 계속합니다: 작업이 성공하거나, 콜백 함수가 nil을 반환하거나, 최대 재시도 횟수에 도달할 때까지입니다. 시스템이 최대 재시도 횟수에 도달하면 충돌을 반환합니다.

데이터 제거

MemoryStoreSortedMap:RemoveAsync()를 사용하여 정렬된 맵에서 하나의 키를 제거하거나 메모리 저장소 정렬된 맵의 모든 데이터를 삭제할 수 있습니다.

키 제거

정렬된 맵에서 키를 제거하려면, 키 이름과 함께 MemoryStoreSortedMap:RemoveAsync()을 호출합니다.

정렬된 맵에서 키 제거

local MemoryStoreService = game:GetService("MemoryStoreService")
local sortedMap = MemoryStoreService:GetSortedMap("SortedMap1")
local setSuccess, _ = pcall(function()
return sortedMap:SetAsync("User_1234", 1000, 30, "someStringSortKey")
end)
if setSuccess then
print("Set succeeded.")
end
local removeSuccess, removeError = pcall(function()
sortedMap:RemoveAsync("User_1234")
end)
if not removeSuccess then
warn(removeError)
end

모든 데이터 삭제

정렬된 맵에서 메모리를 삭제하려면, MemoryStoreSortedMap:GetRangeAsync()로 모든 키를 나열한 후 MemoryStoreSortedMap:RemoveAsync()로 제거합니다.

정렬된 맵에서 메모리 삭제

local MemoryStoreService = game:GetService("MemoryStoreService")
local sortedMap = MemoryStoreService:GetSortedMap("SortedMap1")
-- 초기 하한 값을 nil로 설정하여 첫 번째 항목부터 플러시 시작
local exclusiveLowerBound = nil
while true do
-- 현재 하한에서 최대 100개 항목 가져오기
local getRangeSuccess, items = pcall(function()
return sortedMap:GetRangeAsync(Enum.SortDirection.Ascending, 100, exclusiveLowerBound)
end)
if getRangeSuccess then
local removeSuccess = true
local removeError = nil
for _, item in items do
removeSuccess, removeError = pcall(function()
sortedMap:RemoveAsync(item.key)
end)
end
-- 항목을 제거하는 중 오류가 발생한 경우, 같은 독점 하한으로 다시 시도
if not removeSuccess then
warn(removeError)
-- 항목 수가 100개 미만이면 맵의 끝에 도달함
elseif #items < 100 then
break
else
-- 마지막으로 검색한 키가 다음 반복의 독점 하한이 됨
exclusiveLowerBound = {}
exclusiveLowerBound["key"] = items[#items].key
exclusiveLowerBound["sortKey"] = items[#items].sortKey
end
end
end

크기 가져오기

정렬된 맵에서 항목 수를 가져오려면 MemoryStoreSortedMap:GetSizeAsync()를 호출합니다.

정렬된 맵의 크기 가져오기

local MemoryStoreService = game:GetService("MemoryStoreService")
local sortedMap = MemoryStoreService:GetSortedMap("SortedMap1")
local setSuccess, _ = pcall(function()
return sortedMap:SetAsync("User_1234", 1000, 30, 3.14152)
end)
if setSuccess then
print("Set succeeded.")
end
local size
local success, sizError = pcall(function()
size = sortedMap:GetSizeAsync()
end)
if success then
print(size)
else
warn(sizeError)
end
©2026 Roblox Corporation. Roblox 및 Roblox 로고, 'Powering Imagination'은 미국 및 기타 국가 내 당사의 등록 및 미등록 상표입니다.