메모리 저장소 해시 맵 생성

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

해시 맵 , 정렬된 맵과 유사하게, 메모리에서 키-값 쌍으로 데이터를 저장할 수 있습니다.정렬된 맵과 달리, 순서 보장을 유지하지 않습니다.이 데이터 구조는 공유 인벤토리, 물리적 경매 하우스 등 단순한 데이터 캐싱과 신속한 액세스가 필요한 경우에 유용합니다.해시 맵은 데이터 분할을 자동으로 처리하고 1,000개 이상의 키가 있는 경우 매우 유용합니다.더 작은 키 공간의 경우 정렬된 맵을 권장합니다.

제한

해시 맵에는 128자의 키 크기 제한과 32KB의 값 크기 제한이 있습니다.

그렇지 않으면 해시 맵은 다른 메모리 저장소 데이터 구조와 동일한 API 요청메모리 할당량 제한을 사용합니다.

해시 맵 가져오기

해시 맵을 가져오려면 해시 맵의 이름으로 MemoryStoreService:GetHashMap()를 호출하십시오.이름은 경험 내에서 전역이므로 이 이름을 사용하여 모든 스크립트에서 동일한 해시 맵에 액세스할 수 있습니다.

해시 맵 가져오기

local MemoryStoreService = game:GetService("MemoryStoreService")
local hashMap = MemoryStoreService:GetHashMap("HashMap1")

해시 맵을 가져온 후, 다음 함수 중 하나를 호출하여 해시 맵에서 데이터를 읽거나 쓸 수 있습니다:

함수액션
MemoryStoreHashMap:SetAsync()새 키 추가 또는 키가 이미 존재하는 경우 값을 덮어씁니다.
MemoryStoreHashMap:GetAsync()특정 키를 읽습니다.
MemoryStoreHashMap:ListItemsAsync()해시 맵에서 항목을 목록으로 나열합니다.
MemoryStoreHashMap:UpdateAsync()업데이트 해시 맵에서 검색한 후 키 값을 업데이트합니다.
MemoryStoreHashMap:RemoveAsync()제거 해시 맵에서 키를 제거합니다.

각 함수에 대한 자세한 문서는 MemoryStoreHashMap를 참조하십시오.

데이터 추가 또는 덮어쓰기

새 키를 추가하거나 해시 맵에서 키의 값을 재정의하려면 MemoryStoreHashMap:SetAsync()이름 , 해당 및 초 단위의 만료 시간 을 사용하여 호출합니다.키가 만료되면 메모리가 자동으로 정리됩니다.최대 만료 시간은 3,888,000초(45일)입니다.

해시 맵에 데이터 추가

local MemoryStoreService = game:GetService("MemoryStoreService")
local hashMap = MemoryStoreService:GetHashMap("HashMap1")
local setSuccess, _ = pcall(function()
return hashMap:SetAsync("User_1234", 1000, 30)
end)
if setSuccess then
print("Set succeeded.")
end

데이터 가져오기

특정 키와 관련된 값을 가져오거나 해시 맵에서 여러 키-값 쌍을 가져올 수 있습니다.

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

해시 맵에서 하나의 키와 관련된 값을 가져오려면 MemoryStoreHashMap:GetAsync()이름 으로 호출하십시오.

해시 맵에서 특정 키 가져오기

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

여러 키-값 쌍으로 데이터 가져오기

해시 맵에서 모든 키-값 쌍을 단일 작업으로 가져오려면 원하는 페이지 크기로 MemoryStoreHashMap:ListItemsAsync()를 호출하십시오.이 함수는 페이지화된 방식으로 모든 기존 키를 나열합니다.예를 들어 다음 코드 샘플은 해시 맵에서 최대 32개의 항목을 검색합니다.

해시 맵에 아이템 목록 표시

local MemoryStoreService = game:GetService("MemoryStoreService")
local hashMap = MemoryStoreService:GetHashMap("HashMap1")
-- 아이템 목록 가져오기, 한 번에 32개의 아이템
local success, pages = pcall(function()
return hashMap:ListItemsAsync(32)
end)
if success then
while true do
-- 현재 페이지 가져오기
local entries = pages:GetCurrentPage()
-- 페이지의 모든 키-값 쌍을 반복하여 검사 Iterate through all key-value pairs on page
for _, entry in ipairs(entries) do
print(entry.key .. " : " .. tostring(entry.value))
end
-- 마지막 페이지에 도달했는지 확인
if pages.IsFinished then
break
else
print("----------")
-- 다음 페이지로 이동하기
pages:AdvanceToNextPageAsync()
end
end
end

데이터 업데이트

해시 맵에서 키의 값을 검색하고 업데이트하려면 MemoryStoreHashMap:UpdateAsync()으로 호출 하고, 키를 업데이트하는 콜백 함수 와 초 단위의 만료 시간 을 사용하십시오.

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

예를 들어 다음 코드 샘플은 공유 인벤토리의 리소스 수를 업데이트합니다.UpdateAsync() 모든 플레이어 기여가 동시에 이루어져도 이 공유 인벤토리에 들어갈 수 있도록 합니다.이 함수에서는 또한 최대 리소스 수 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
-- 최대 리소스 수를 초과하지 않도록 확인
if resource.count > 500 then
resource.count = 500
end
return resource
end, 1200)
end)
if success then
print(newResourceCount)
end
end

지연 시간은 UpdateAsync()GetAsync()SetAsync() 경쟁이 없는 경우와 비슷합니다.

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

데이터 제거

해시 맵에서 하나의 키를 제거하고 메모리 저장소 해시 맵에서 모든 데이터를 삭제하기 위해 MemoryStoreHashMap:RemoveAsync()를 사용할 수 있습니다.

키 제거

해시 맵에서 키를 제거하려면 키 MemoryStoreHashMap:RemoveAsync() 으로 이름 을 호출하십시오.

해시 맵에서 키 제거

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

모든 데이터 삭제

해시 맵에서 모든 데이터를 삭제하려면 MemoryStoreHashMap:ListItemsAsync()로 모든 항목을 나열한 다음 MemoryStoreHashMap:RemoveAsync()로 제거합니다.

해시 맵의 모든 데이터 삭제

local MemoryStoreService = game:GetService("MemoryStoreService")
local hashMap = MemoryStoreService:GetHashMap("HashMap1")
-- 아이템 목록 가져오기, 한 번에 32개의 아이템
local success, pages = pcall(function()
return hashMap:ListItemsAsync(32)
end)
if success then
while true do
-- 현재 페이지 가져오기
local entries = pages:GetCurrentPage()
local removeSuccess = true
local removeError = nil
-- 페이지의 모든 키-값 쌍을 반복하여 검사 Iterate through all key-value pairs on page
for _, entry in ipairs(entries) do
print(entry.key .. " : " .. tostring(entry.value))
removeSuccess, removeError = pcall(function()
hashMap:RemoveAsync(entry.key)
end)
if not removeSuccess then
warn(removeError)
end
end
-- 마지막 페이지에 도달했는지 확인
if pages.IsFinished then
print("Finished deleting all data.")
break
else
print("----------")
-- 다음 페이지로 이동하기
pages:AdvanceToNextPageAsync()
end
end
end