メインコンテンツまでスキップ

Sorted set collections

Momento Cacheのソート済みセットは、値(String、Byte[]など)とスコア(符号付きダブル64ビットfloat)のペアを持つ一意の要素のコレクションです。ソートされたセットの要素は、スコア順に並べられます。

備考

Momento コレクションタイプは、CollectionTTLを使用してTTL動作を指定します。これは、すべての "write" 操作のオプション引数です。

Sorted set methods

SortedSetPutElement

ソートされたセットに新しい要素を追加したり、既存のソートされたセット要素を更新したりします。

  • セットが存在しない場合、このメソッドは渡された要素を持つ新しいソート済みセットコレクションを作成します。

  • セットが存在する場合、その が存在しなければ、その要素はソートされたセットに追加されます。その要素の値が存在する場合、その要素は上書きされます。

NameTypeDescription
cacheNameStringキャッシュの名前
setNameString変更するソートセットコレクションの名前。
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 collection. This TTL takes precedence over the TTL used when initializing a cache connection client.
Method response object
  • Success
  • Error

詳しくはレスポンスオブジェクトを参照してください。

const result = await cacheClient.sortedSetPutElement(cacheName, 'test-sorted-set', 'test-value', 5);
switch (result.type) {
case CacheSortedSetPutElementResponse.Success:
console.log("Value 'test-value' with score '5' added successfully to sorted set 'test-sorted-set'");
break;
case CacheSortedSetPutElementResponse.Error:
throw new Error(
`An error occurred while attempting to call cacheSortedSetPutElement on sorted set 'test-sorted-set' in cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}

SortedSetPutElements

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

  • If the set does not exist, this method creates a new sorted set collection 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
cacheNameStringキャッシュの名前
setNameString変更するソートセットコレクションの名前。
elementsSortedSetElement[]この操作によってソートされたセットに追加される要素。
ttlCollectionTTL objectソートされたセットコレクションの TTL。この TTL は、キャッシュ接続クライアントを初期化するときに使用される TTL よりも優先されます。
Method response object
  • Success
  • Error

詳しくはレスポンスオブジェクトを参照してください。

const result = await cacheClient.sortedSetPutElements(
cacheName,
'test-sorted-set',
new Map<string, number>([
['key1', 10],
['key2', 20],
])
);
switch (result.type) {
case CacheSortedSetPutElementsResponse.Success:
console.log("Elements added successfully to sorted set 'test-sorted-set'");
break;
case CacheSortedSetPutElementsResponse.Error:
throw new Error(
`An error occurred while attempting to call cacheSortedSetPutElements on sorted set 'test-sorted-set' in cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}

SortedSetFetchByRank

ソートされた集合の要素を取得し、オプションで順位でフィルタリングして、昇順または降順で返します。

NameTypeDescription
cacheNameStringキャッシュの名前
setNameStringソートされたセットコレクションの名前。
startRankOptional[integer]結果の開始順位。デフォルトはゼロです。
endRankOptional[integer]結果の排他的な終了順位。デフォルトは null である。
orderAscending | Descendingソートされたセットを返したい順番。
Method response object
  • Hit
    • elements(): SortedSetElement[]
  • Miss
  • Error

詳しくはレスポンスオブジェクトを参照してください。

await cacheClient.sortedSetPutElements(
cacheName,
'test-sorted-set',
new Map<string, number>([
['key1', 10],
['key2', 20],
])
);
const result = await cacheClient.sortedSetFetchByRank(cacheName, 'test-sorted-set');

// simplified style; assume the value was found
console.log(`Sorted set fetched: ${result.value()!}`);

// pattern-matching style; safer for production code
switch (result.type) {
case CacheSortedSetFetchResponse.Hit:
console.log("Values from sorted set 'test-sorted-set' fetched by rank successfully- ");
result.value().forEach(res => {
console.log(`${res.value} : ${res.score}`);
});
break;
case CacheSortedSetFetchResponse.Miss:
console.log(`Sorted Set 'test-sorted-set' was not found in cache '${cacheName}'`);
break;
case CacheSortedSetFetchResponse.Error:
throw new Error(
`An error occurred while attempting to call cacheSortedSetFetchByRank on sorted set 'test-sorted-set' in cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}

SortedSetFetchByScore

ソートされた集合の要素を取得し、スコアでフィルタリングして昇順または降順で返します。

NameTypeDescription
cacheNameStringキャッシュの名前
setNameStringソートされたセットコレクションの名前。
minScoreOptional[double]結果の低スコアを含む。デフォルトは -inf で、最低スコアまで含める。
maxScoreOptional[double]結果のハイスコア。デフォルトは +inf である。
orderAscending | Descendingソートされたセットを返したい順番。
offsetOptional[int]フィルタリングされたリストから結果を返し始めるオフセット。デフォルトは0、つまりフィルタリングしない。指定する場合は、非負でなければならない。
countOptional[int]フィルタリングされたリストから返す結果の最大数。デフォルトは null で、つまり無制限である。指定する場合は、厳密に正数でなければならない。
Method response object
  • Hit
    • elements(): SortedSetElement[]
  • Miss
  • Error

詳しくはレスポンスオブジェクトを参照してください。

await cacheClient.sortedSetPutElements(
cacheName,
'test-sorted-set',
new Map<string, number>([
['key1', 100],
['key2', 25],
])
);
const result = await cacheClient.sortedSetFetchByScore(cacheName, 'test-sorted-set');

// simplified style; assume the value was found
console.log(`Fetched values from sorted set: ${result.value()!}`);

// pattern-matching style; safer for production code
switch (result.type) {
case CacheSortedSetFetchResponse.Hit:
console.log("Values from sorted set 'test-sorted-set' fetched by score successfully- ");
result.value().forEach(res => {
console.log(`${res.value} : ${res.score}`);
});
break;
case CacheSortedSetFetchResponse.Miss:
console.log(`Sorted Set 'test-sorted-set' was not found in cache '${cacheName}'`);
break;
case CacheSortedSetFetchResponse.Error:
throw new Error(
`An error occurred while attempting to call cacheSortedSetFetchByScore on sorted set 'test-sorted-set' in cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}

SortedSetGetScore

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

NameTypeDescription
cacheNameStringキャッシュの名前
setNameStringソートされたセットコレクションの名前。
valueString | Bytesスコアを取得する値。
Method response object
  • Cache hit
    • Score: number
  • Cache miss (if the sorted set does not exist)
  • Error

詳しくはレスポンスオブジェクトを参照してください。

await cacheClient.sortedSetPutElements(
cacheName,
'test-sorted-set',
new Map<string, number>([
['key1', 10],
['key2', 20],
])
);
const result = await cacheClient.sortedSetGetScore(cacheName, 'test-sorted-set', 'key1');

// simplified style; assume the value was found
console.log(`Element with value 'key1' has score: ${result.score()!}`);

// pattern-matching style; safer for production code
switch (result.type) {
case CacheSortedSetGetScoreResponse.Hit:
console.log(`Element with value 'key1' has score: ${result.score()}`);
break;
case CacheSortedSetGetScoreResponse.Miss:
console.log(`Sorted Set 'test-sorted-set' was not found in cache '${cacheName}'`);
break;
case CacheSortedSetGetScoreResponse.Error:
throw new Error(
`An error occurred while attempting to call cacheSortedSetFetchGetScore on sorted set 'test-sorted-set' in cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}

SortedSetGetScores

ソートされたセットから、値でインデックス付けされた要素のリストに関連付けられたスコアを取得します。

NameTypeDescription
cacheNameStringキャッシュの名前
setNameStringソートされたセットコレクションの名前。
valuesString[] | Bytes[]スコアを取得する値の配列。
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

詳しくはレスポンスオブジェクトを参照してください。

await cacheClient.sortedSetPutElements(
cacheName,
'test-sorted-set',
new Map<string, number>([
['key1', 10],
['key2', 20],
])
);
const result = await cacheClient.sortedSetGetScores(cacheName, 'test-sorted-set', ['key1', 'key2']);

// simplified style; assume the value was found
console.log(`Retrieved scores from sorted set: ${result.value()!}`);

// pattern-matching style; safer for production code
switch (result.type) {
case CacheSortedSetGetScoresResponse.Hit:
console.log('Element scores retrieved successfully -');
result.valueMap().forEach((value, key) => {
console.log(`${key} : ${value}`);
});
break;
case CacheSortedSetGetScoresResponse.Miss:
console.log(`Sorted Set 'test-sorted-set' was not found in cache '${cacheName}'`);
break;
case CacheSortedSetGetScoresResponse.Error:
throw new Error(
`An error occurred while attempting to call cacheSortedSetFetchGetScores on sorted set 'test-sorted-set' in cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}

SortedSetRemoveElement

値でインデックス付けされた、ソートされたセットから要素を削除します。

NameTypeDescription
cacheNameStringキャッシュの名前
setNameString変更するセットコレクションの名前。
valueString | Bytesこの操作によって削除される要素の値。
Method response object
  • Success
  • Error

詳しくはレスポンスオブジェクトを参照してください。

await cacheClient.sortedSetPutElement(cacheName, 'test-sorted-set', 'test-value', 10);
const result = await cacheClient.sortedSetRemoveElement(cacheName, 'test-sorted-set', 'test-value');
switch (result.type) {
case CacheSortedSetRemoveElementResponse.Success:
console.log("Element with value 'test-value' removed successfully");
break;
case CacheSortedSetRemoveElementResponse.Error:
throw new Error(
`An error occurred while attempting to call cacheSortedSetRemoveElement on sorted set 'test-sorted-set' in cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}

SortedSetRemoveElements

値でインデックス付けされたソートされたセットから要素を削除します。

NameTypeDescription
cacheNameStringキャッシュの名前
setNameString変更するセットコレクションの名前。
valuesString[] | Bytes[]この操作によって削除される要素の値。

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

Method response object
  • Success
  • Error

詳しくはレスポンスオブジェクトを参照してください。

await cacheClient.sortedSetPutElements(
cacheName,
'test-sorted-set',
new Map<string, number>([
['key1', 10],
['key2', 20],
])
);
const result = await cacheClient.sortedSetRemoveElements(cacheName, 'test-sorted-set', ['key1', 'key2']);
switch (result.type) {
case CacheSortedSetRemoveElementsResponse.Success:
console.log("Elements with value 'key1' and 'key2 removed successfully");
break;
case CacheSortedSetRemoveElementsResponse.Error:
throw new Error(
`An error occurred while attempting to call cacheSortedSetRemoveElements on sorted set 'test-sorted-set' in cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}

SortedSetGetRank

指定されたソートされた集合の中で、要素はどの位置にあるか?が分かります。

NameTypeDescription
cacheNameStringキャッシュの名前
setNameString変更するソートセットコレクションの名前。
valueString | Bytesスコアを取得する要素の値。
Method response object
  • Hit
    • Rank: integer
  • Miss
  • Error

詳しくはレスポンスオブジェクトを参照してください。

await cacheClient.sortedSetPutElements(
cacheName,
'test-sorted-set',
new Map<string, number>([
['key1', 10],
['key2', 20],
['key3', 30],
])
);
const result = await cacheClient.sortedSetGetRank(cacheName, 'test-sorted-set', 'key2');

// simplified style; assume the value was found
console.log(`Element with value 'key1' has rank: ${result.rank()!}`);

// pattern-matching style; safer for production code
switch (result.type) {
case CacheSortedSetGetRankResponse.Hit:
console.log(`Element with value 'key1' has rank: ${result.rank()}`);
break;
case CacheSortedSetGetRankResponse.Miss:
console.log(`Sorted Set 'test-sorted-set' was not found in cache '${cacheName}'`);
break;
case CacheSortedSetGetRankResponse.Error:
throw new Error(
`An error occurred while attempting to call cacheSortedSetFetchGetRank on sorted set 'test-sorted-set' in cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}

SortedSetIncrementScore

要素のスコアを加算します。値がソートされたセットから missing されている場合、このメソッドは値をインクリメントする量に設定します。

注記

インクリメンタルされた後のスコアは、-9223372036854775808から9223372036854775807までの範囲内である必要があります(符号付きの64ビット浮動小数点数)。範囲外の場合は、エラーレスポンスが返されます。

例:

  • 要素がソートされたセットに存在しない場合、SortedSetIncrementScore(cacheName, setName, value, 10) は要素のスコアを10に設定します。
  • 既存の要素が value:score の "{ 'KesselRun' : 12 }" の場合、SortedSetIncrementScore(cacheName, setName, 10) は要素のスコアを10に設定します。の場合、SortedSetIncrementScore(cacheName, setName, value, 10) は要素のスコアを22に設定します。
NameTypeDescription
cacheNameStringキャッシュの名前
setNameString変更するソートセットコレクションの名前。
valueString | Bytesこの操作によってインクリメントされる要素の値。
amountNumberスコアに加算する量。正、負、ゼロのいずれかを指定する。デフォルトは1。
ttlCollectionTTL objectソートされたセット・コレクションの TTL。この TTL は、キャッシュ接続クライアントを初期化するときに使用される TTL よりも優先されます。
Method response object
  • Success
    • Value: number - the new value after incrementing
  • Error

詳しくはレスポンスオブジェクトを参照してください。

await cacheClient.sortedSetPutElement(cacheName, 'test-sorted-set', 'test-value', 10);
const result = await cacheClient.sortedSetIncrementScore(cacheName, 'test-sorted-set', 'test-value', 1);
switch (result.type) {
case CacheSortedSetIncrementScoreResponse.Success:
console.log(`Score for value 'test-value' incremented successfully. New score - ${result.score()}`);
break;
case CacheSortedSetIncrementScoreResponse.Error:
throw new Error(
`An error occurred while attempting to call cacheSortedSetIncrementScore on sorted set 'test-sorted-set' in cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}

SortedSetElement

ソートされた集合の各要素を構成するのは、値とスコアです。

例: { "TomHocusXaster" : 1138 }

NameTypeDescription
ValueString | Bytesソートされたセット要素の値。
ScoreSigned double 64-bit float要素に得点をつける。

SortedSetElementは、それ自体で存在することもできるし、SortedSetElementの配列の一部として存在することもできます。

SortedSetLength

ソートされたセットコレクションのエントリ数を取得します。

NameTypeDescription
cacheNameStringキャッシュの名前
sortedSetNameStringチェックするソートセットコレクションの名前。
Method response object
  • Hit
    • length(): Number
  • Miss
  • Error

詳しくはレスポンスオブジェクトを参照してください。

  let _length: u32 = cache_client
.sorted_set_length(cache_name, "sorted_set_name")
.await?
.try_into()
.expect("Expected a list length!");

SortedSetLengthByScore

既存のソートされた集合コレクションに対して、指定された最小スコアと最大スコアの間のすべての値を見つけ、その長さを返します。

NameTypeDescription
cacheNameStringキャッシュの名前
sortedSetNameStringチェックするソートセットコレクションの名前。
minScoreOptional[double]結果の低スコアを含む。デフォルトは -inf で、最低スコアまで含める。
maxScoreOptional[double]結果のハイスコア。デフォルトは +inf である。
Method response object
  • Hit
    • length(): Number
  • Miss
  • Error

詳しくはレスポンスオブジェクトを参照してください。