Skip to main content

Sorted set collections

A sorted set in Momento Cache is a collection of unique elements with a value (string, byte[], etc.) and score (signed double 64-bit float) pair. The elements in the item are ordered by the score.

Sorted set methods

SortedSetPutElement

Adds a new or updates an existing sorted set element in a sorted set item.

  • If the set does not exist, this method creates a new sorted set item with the element passed in.

  • If the set exists, the element is added to the sorted set if that value doesn't exist. If the value of that element does exist, that element is overwritten.

NameTypeDescription
cacheNameStringName of the cache.
setNameStringName of the sorted set item to be altered.
valueString | byte[]The value of the element to be added to the sorted set by this operation.
scorenumberThe score of the element to be added to the sorted set by this operation.
ttlCollectionTTL objectTTL for the sorted set item. This TTL takes precedence over the TTL used when initializing a cache connection client.
Method response object
  • Success
  • Error

See response objects for specific information.

SortedSetPutElements

Adds new or updates existing sorted set elements in a sorted set item.

  • If the set does not exist, this method creates a new sorted set item with the element(s) passed in.

  • If the set exists, for each SortedSetElement in the array, each element is added to the sorted set if that value doesn't exist. If the value of that element does exist, that element is overwritten.

NameTypeDescription
cacheNameStringName of the cache.
setNameStringName of the sorted set item to be altered.
elementsSortedSetElement[]Elements to be added to the sorted set by this operation.
ttlCollectionTTL objectTTL for the sorted set item. This TTL takes precedence over the TTL used when initializing a cache connection client.
Method response object
  • Success
  • Error

See response objects for specific information.

SortedSetFetchByRank

Fetch elements of sorted set, optionally filtered by rank, and return them in ascending or descending order.

NameTypeDescription
cacheNameStringName of the cache.
setNameStringName of the sorted set item.
startRankOptional[integer]The inclusive start rank of the results. Default is zero.
endRankOptional[integer]The exclusive end rank of the results. Default is null, ie up to and including the element ranked last.
orderAscending | DescendingThe order you want the sorted set returned.
Method response object
  • Hit
    • elements(): SortedSetElement[]
  • Miss
  • Error

See response objects for specific information.

SortedSetFetchByScore

Fetch elements of sorted set, optionally filtered by score, and return them in ascending or descending order.

NameTypeDescription
cacheNameStringName of the cache.
setNameStringName of the sorted set item.
minScoreOptional[double]The inclusive low score of the results. Default is -inf, ie include through the lowest score.
maxScoreOptional[double]The inclusive high score of the results. Default is +inf, ie include through the highest score.
orderAscending | DescendingThe order you want the sorted set returned.
offsetOptional[int]The offset, inclusive, into the filtered list from which to start returning results. Default is 0, ie do not filter. If specified, must be non-negative.
countOptional[int]The maximum number of results from the filtered list to return. Default is null, ie no limit. If specified, must be strictly positive.
Method response object
  • Hit
    • elements(): SortedSetElement[]
  • Miss
  • Error

See response objects for specific information.

SortedSetGetScore

Gets an element's score from the sorted set, indexed by value.

NameTypeDescription
cacheNameStringName of the cache.
setNameStringName of the sorted set item.
valueString | bytesThe value to get the score of.
Method response object
  • Cache hit
    • Score: number
  • Cache miss (if the sorted set does not exist)
  • Error

See response objects for specific information.

SortedSetGetScores

Gets the scores associated with a list of elements from the sorted set, indexed by value.

NameTypeDescription
cacheNameStringName of the cache.
setNameStringName of the sorted set item.
valuesString[] | bytes[]An array of values to get the score of.
Method response object
  • Cache hit
    • Elements() (returns hit/miss per element)
      • Hit:
        • Score: number
      • Miss
  • Cache miss (if the sorted set does not exist)
  • Error

See response objects for specific information.

SortedSetRemoveElement

Removes an element from a sorted set, indexed by value.

NameTypeDescription
cacheNameStringName of the cache.
setNameStringName of the set item to be altered.
valueString | bytesValue of the element to be removed by this operation.
Method response object
  • Success
  • Error

See response objects for specific information.

SortedSetRemoveElements

Removes elements from a sorted set, indexed by values.

NameTypeDescription
cacheNameStringName of the cache.
setNameStringName of the set item to be altered.
valuesString[] | bytes[]Values of the elements to be removed by this operation.

You can remove either one or a specific group of elements.

Method response object
  • Success
  • Error

See response objects for specific information.

SortedSetGetRank

What position is the element, in the specified sorted set?

NameTypeDescription
cacheNameStringName of the cache.
setNameStringName of the sorted set item to be altered.
valueString | bytesValue of the element to retrieve the score of.
Method response object
  • Hit
    • Rank: integer
  • Miss
  • Error

See response objects for specific information.

SortedSetIncrementScore

Adds to the score of an element. If the value is missing from the sorted set, this method sets the value to the amount to increment by.

note

The resulting incremented score must be between -9223372036854775808 and 9223372036854775807, ie. a signed double 64-bit float. If not, there will be an error response.

Examples:

  • When the element does not exist in the sorted set, SortedSetIncrementScore(cacheName, setName, value, 10) will set the element's score to 10.
  • When the existing element is a value:score of "{ 'KesselRun' : 12 }" , SortedSetIncrementScore(cacheName, setName, value, 10) will set the element's score to 22.
NameTypeDescription
cacheNameStringName of the cache.
setNameStringName of the sorted set item to be altered.
valueString | bytesValue for the element to be incremented by this operation.
amountNumberThe quantity to add to the score. May be positive, negative, or zero. Defaults to 1.
ttlCollectionTTL objectTTL for the sorted set item. This TTL takes precedence over the TTL used when initializing a cache connection client.
Method response object
  • Success
    • Value: number - the new value after incrementing
  • Error

See response objects for specific information.

SortedSetElement

A value and score makes up each element in a sorted set.

Example: { "TomHocusXaster" : 1138 }

NameTypeDescription
ValueString | bytesValue for the sorted set element.
ScoreSigned double 64-bit floatScore the element.

A SortedSetElement can exist by itself or as part of an array of SortedSetElements