정렬된 메모리 저장소 의 데이터 구조는 선택적 정렬 키를 사용하여 자주 사용되는 메모리 데이터를 키-값 쌍으로 저장하고 정렬 키와 키에 따라 특정 순서를 유지할 수 있습니다.큐와 달리, 맵에 들어가는 키의 순서는 처리 순서를 결정하지 않으므로 정렬된 맵은 리더보드 및 크로스 서버 경매와 같은 참여 엔터티를 구현하기 위한 데이터 조직 정렬에 유용합니다.
제한
데이터 구조 크기 제한 외에도, 정렬된 맵에는 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() | 키를 정렬된 맵에서 제거합니다. |
데이터 추가 또는 덮어쓰기
정렬된 맵에서 새 키를 추가하거나 키의 값 또는 정렬 키를 재정의하려면 키 이름 , 해당 값 , 초 단위의 만료 시간 및 선택적 정렬 키 를 사용하여 호출하십시오.키가 만료되면 메모리가 자동으로 정리됩니다.최대 만료 시간은 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 모든 숫자 정렬 키가 있는 키 후에 정렬됩니다.player4 및 player5 은 동일한 종류 키를 가지고 있으므로 키 순서대로 정렬됩니다.
정렬된 맵에 데이터 추가
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()를 호출하십시오.이 함수는 기본적으로 모든 기존 키를 나열하지만, 키 범위의 상하한도를 설정할 수 있습니다.예를 들어, 다음 코드 샘플은 키가 10 보다 크거나 같은 20개의 항목을 정렬된 맵의 시작부터 검색하며, 정렬 키가 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일)입니다.
대부분의 경험에서 여러 서버가 동시에 동일한 키를 업데이트하고 값을 변경할 수 있습니다.As UpdateAsync() 항상 업데이트하기 전에 최신 값을 수정하므로, 최신 값을 콜백 함수의 입력으로 읽도록 사용해야 합니다.
예를 들어 다음 코드 샘플은 게임의 플레이어에 대한 리더보드 점수를 업데이트합니다.점수는 킬/데스로 계산됩니다.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`는 맵에서 항목을 정렬하는 데 사용되는 정렬 키입니다
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
지연 시간은 UpdateAsync() 과 GetAsync() 및 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")
-- 공백의 최초 하한은 첫 번째 아이템플러시됩니다
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