MemoryStoreSortedMap
Provides access to a sorted map within MemoryStore. A sorted map is a collection of items where string keys are associated with arbitrary values (up to the maximum allowed size -- see Memory Stores. The keys are arranged in alphabetical order.
Summary
Properties
Methods
Retrieves items within a sorted range of keys.
Retrieves the value of a key from a sorted map and updates it with a new value.
Events
Properties
Methods
GetRangeAsync
Gets items within a sorted range of keys.
Parameters
Sort direction, ascending or descending.
The number of items to retrieve; the maximum allowed value for this parameter is 200.
(Optional) Lower bound, exclusive, for the returned keys.
(Optional) Upper bound, exclusive, for the returned keys.
Returns
Item keys and values in the requested range.
Code Samples
local MemoryStoreService = game:GetService("MemoryStoreService")
local myMap = MemoryStoreService:GetSortedMap("MySortedMap")
function printAllKeys(map)
-- the initial lower bound is nil which means to start from the very first item
local exclusiveLowerBound = nil
-- this loop continues until the end of the map is reached
while true do
-- get up to a hundred items starting from the current lower bound
local items = map:GetRangeAsync(Enum.SortDirection.Ascending, 100, exclusiveLowerBound)
for _, item in ipairs(items) do
print(item.key)
end
-- if the call returned less than a hundred items it means we've reached the end of the map
if #items < 100 then
break
end
-- the last retrieved key is the exclusive lower bound for the next iteration
exclusiveLowerBound = items[#items].key
end
end
printAllKeys(myMap)
RemoveAsync
Returns
SetAsync
Sets the value of the key overwriting any existing key value.
Returns
True if a new item was added, false -- if an existing item was updated.
UpdateAsync
Retrieves the value of a key from a sorted map and lets you update it to a new value via a callback function.
This method accepts a callback function that transforms the old value into the updated value as required. The method retrieves the existing key value and passes it to the transform function which returns the new value for the item, with these exceptions:
- If the key does not exist, the old value passed to the function will be nil.
- If the function returns nil, the update is canceled.
The new value is saved only if the key was not updated (e.g. by a different game server) since the moment it was read. If the value did change, the transform function is invoked again with the most recent item value. This cycle repeats until the value is saved successfully or the transform function returns nil to abort the operation.
Parameters
Key whose value to update.
A function which you need to provide. The function takes the key's old value as input and returns the new value.
Item expiration time, in seconds, after which the item will be automatically removed from the sorted map. The maximum expiration time is 45 days (3,888,000 seconds).
Returns
The return value is the last value returned by the transform function.
Code Samples
local MemoryStoreService = game:GetService("MemoryStoreService")
local map = MemoryStoreService:GetSortedMap("AuctionItems")
function placeBid(itemKey, bidAmount)
map:UpdateAsync(itemKey, function(item)
item = item or { highestBid = 0 }
if item.highestBid < bidAmount then
item.highestBid = bidAmount
return item
end
return nil
end)
end
placeBid("MyItem", 50)