MemoryStoreSortedMap

Show Deprecated
Not Creatable
Not Replicated

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

GetAsync(key: string): Variant  YIELDS

Retrieves the value of a key in the sorted map.

GetRangeAsync(direction: SortDirection, count: number, exclusiveLowerBound: string, exclusiveUpperBound: string): Array  YIELDS

Retrieves items within a sorted range of keys.

RemoveAsync(key: string): void  YIELDS

Removes the provided key from the sorted map.

SetAsync(key: string, value: Variant, expiration: number): boolean  YIELDS

Sets the value of a key.

UpdateAsync(key: string, transformFunction: function, expiration: number): Variant  YIELDS

Retrieves the value of a key from a sorted map and updates it with a new value.

Events

Properties

Methods

GetAsync

Yields

Retrieves the value of a key in the sorted map.

Parameters

key: string

Key whose value to retrieve.


Returns

Key value, or nil -- if there's no item with the specified key.

GetRangeAsync

Yields

Gets items within a sorted range of keys.

Parameters

direction: SortDirection

Sort direction, ascending or descending.

count: number

The number of items to retrieve; the maximum allowed value for this parameter is 200.

exclusiveLowerBound: string

(Optional) Lower bound, exclusive, for the returned keys.

Default Value: ""
exclusiveUpperBound: string

(Optional) Upper bound, exclusive, for the returned keys.

Default Value: ""

Returns

Item keys and values in the requested range.

Code Samples

Retrieving MemoryStore Keys

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

void
Yields

Removes the provided key from the sorted map.

Parameters

key: string

Key to remove.


Returns

void

SetAsync

Yields

Sets the value of the key overwriting any existing key value.

Parameters

key: string

Key whose value to set.

value: Variant

Key value to set.

expiration: number

Item expiration, in seconds. The item is automatically removed from the sorted map once the expiration duration is reached. The maximum expiration time is 30 days (2,592,000 seconds).


Returns

True if a new item was added, false -- if an existing item was updated.

UpdateAsync

Yields

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: string

Key whose value to update.

transformFunction: function

A function which you need to provide. The function takes the key's old value as input and returns the new value.

expiration: number

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

Updating a MemoryStore

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)

Events