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

Sorted set collections

Momento Cache におけるソート済みのセットは、値(String, Byte[], など)とスコア(符号付き64ビット浮動小数点数)のペアを持つユニークな要素の集まりです。アイテム内の要素はスコアによって順序付けられています。

Sorted set メソッド

SortedSetPutElement

ソート済みセットのアイテムに新たな ソート済みセットの要素 を追加するか、既存の要素を更新します。

  • セットが存在しない場合、このメソッドは渡された要素を含む新たなソート済みセットのアイテムを作成します。

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

名前説明
cacheNameStringキャッシュの名前
setNameString変更対象のソート済みセットアイテムの名前
valueString | Byte[]この操作によってソート済みセットに追加される要素の値。
scorenumberこの操作によってソート済みセットに追加される要素のスコア。
ttlCollectionTTL objectソート済みセットアイテムのTTL(Time to Live: 生存時間)。このTTLは、キャッシュ接続クライアントを初期化する際に使用されるTTLよりも優先されます。
Method response object
  • Success
  • Error

特定の情報については、 レスポンスオブジェクト を参照してください。

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

SortedSetPutElements

ソート済みセットのアイテムに新たなソート済みセットの要素を追加するか、既存の要素を更新します

  • セットが存在しない場合、このメソッドは渡された要素(要素たち)を含む新たなソート済みセットのアイテムを作成します。

  • セットが存在する場合、配列内の各SortedSetElementについて、その値が存在しなければ要素はソート済みセットに追加されます。その要素の値が存在する場合、その要素は上書きされます。

名前説明
cacheNameStringキャッシュの名前
setNameString変更対象のソート済みセットアイテムの名前
elementsSortedSetElement[]この操作によってソート済みセットに追加される要素。
ttlCollectionTTL objectソート済みセットアイテムのTTL(Time to Live: 生存時間)。このTTLは、キャッシュ接続クライアントを初期化する際に使用されるTTLよりも優先されます。
Method response object
  • Success
  • Error

特定の情報については、レスポンスオブジェクトを参照してください。

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

SortedSetFetchByRank

ランクによるフィルタリングをオプションで適用しつつ、ソート済みセットの要素を取得し、昇順または降順で返します。

名前説明
cacheNameStringキャッシュの名前
setNameStringソート済みセットアイテムの名前
startRankOptional[integer]開始のランクの結果(を含む)。デフォルトは0となっています。
endRankOptional[integer]終了のランク(を含まない)。デフォルトはnull。最後のランクまでの要素を含む。
orderAscending | Descendingソート済みセットを返す順序。
Method response object
  • Hit
    • elements(): SortedSetElement[]
  • Miss
  • Error

特定の情報については、レスポンスオブジェクトを参照してください。

await cacheClient.sortedSetPutElements(
'test-cache',
'test-sorted-set',
new Map<string, number>([
['key1', 10],
['key2', 20],
])
);
const result = await cacheClient.sortedSetFetchByRank('test-cache', 'test-sorted-set');
if (result instanceof CacheSortedSetFetch.Hit) {
console.log("Values from sorted set 'test-sorted-set' fetched by rank successfully- ");
result.valueArray().forEach(res => {
console.log(`${res.value} : ${res.score}`);
});
} else if (result instanceof CacheSortedSetFetch.Miss) {
console.log("Sorted Set 'test-sorted-set' was not found in cache 'test-cache'");
} else if (result instanceof CacheSortedSetFetch.Error) {
throw new Error(
`An error occurred while attempting to call cacheSortedSetFetchByRank on sorted set 'test-sorted-set' in cache 'test-cache': ${result.errorCode()}: ${result.toString()}`
);
}

SortedSetFetchByScore

スコアによるフィルタリングをオプションで適用しつつ、ソート済みセットの要素を取得し、昇順または降順で返します。

名前説明
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(
'test-cache',
'test-sorted-set',
new Map<string, number>([
['key1', 100],
['key2', 25],
])
);
const result = await cacheClient.sortedSetFetchByScore('test-cache', 'test-sorted-set');
if (result instanceof CacheSortedSetFetch.Hit) {
console.log("Values from sorted set 'test-sorted-set' fetched by score successfully- ");
result.valueArray().forEach(res => {
console.log(`${res.value} : ${res.score}`);
});
} else if (result instanceof CacheSortedSetFetch.Miss) {
console.log("Sorted Set 'test-sorted-set' was not found in cache 'test-cache'");
} else if (result instanceof CacheSortedSetFetch.Error) {
throw new Error(
`An error occurred while attempting to call cacheSortedSetFetchByScore on sorted set 'test-sorted-set' in cache 'test-cache': ${result.errorCode()}: ${result.toString()}`
);
}

SortedSetGetScore

ソート済みセットから、値によってインデックス付けられた要素のスコアを取得します。

名前説明
cacheNameStringキャッシュの名前
setNameStringソート済みセットアイテムの名前
valueString | Bytesスコアを取得する値。
Method response object
  • Cache hit
    • Score: number
  • Cache miss (if the sorted set does not exist)
  • Error

特定の情報については、レスポンスオブジェクトを参照してください。

await cacheClient.sortedSetPutElements(
'test-cache',
'test-sorted-set',
new Map<string, number>([
['key1', 10],
['key2', 20],
])
);
const result = await cacheClient.sortedSetGetScore('test-cache', 'test-sorted-set', 'key1');
if (result instanceof CacheSortedSetGetScore.Hit) {
console.log(`Element with value 'key1' has score: ${result.score()}`);
} else if (result instanceof CacheSortedSetGetScore.Miss) {
console.log("Sorted Set 'test-sorted-set' was not found in cache 'test-cache'");
} else if (result instanceof CacheSortedSetGetScore.Error) {
throw new Error(
`An error occurred while attempting to call cacheSortedSetFetchGetScore on sorted set 'test-sorted-set' in cache 'test-cache': ${result.errorCode()}: ${result.toString()}`
);
}

SortedSetGetScores

ソート済みセットから、値によってインデックス付けられた要素のスコアを取得します。複数の要素に関連するスコアを取得します。

名前説明
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(
'test-cache',
'test-sorted-set',
new Map<string, number>([
['key1', 10],
['key2', 20],
])
);
const result = await cacheClient.sortedSetGetScores('test-cache', 'test-sorted-set', ['key1', 'key2']);
if (result instanceof CacheSortedSetGetScores.Hit) {
console.log('Element scores retrieved successfully -');
result.valueMap().forEach((value, key) => {
console.log(`${key} : ${value}`);
});
} else if (result instanceof CacheSortedSetGetScores.Miss) {
console.log("Sorted Set 'test-sorted-set' was not found in cache 'test-cache'");
} else if (result instanceof CacheSortedSetGetScores.Error) {
throw new Error(
`An error occurred while attempting to call cacheSortedSetFetchGetScores on sorted set 'test-sorted-set' in cache 'test-cache': ${result.errorCode()}: ${result.toString()}`
);
}

SortedSetRemoveElement

値によってインデックス付けられたソート済みセットから要素を削除します。

名前説明
cacheNameStringキャッシュの名前
setNameStringName of the set item to be altered.
valueString | Bytesこの操作によって削除される要素の値。
Method response object
  • Success
  • Error

特定の情報については、レスポンスオブジェクトを参照してください。

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

SortedSetRemoveElements

値によってインデックス付けられたソート済みセットから要素を削除します。

名前説明
cacheNameStringキャッシュの名前
setNameString変更対象のセットアイテムの名前。
valuesString[] | Bytes[]この操作によって削除される要素の値。

これにより、個別の要素または特定のグループの要素を削除することができます。

Method response object
  • Success
  • Error

特定の情報については、レスポンスオブジェクトを参照してください。

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

SortedSetGetRank

指定されたソート済みセット内の要素の位置は何番目かを知ることができます。

名前説明
cacheNameStringキャッシュの名前
setNameString変更対象のソート済みセットアイテムの名前。
valueString | Bytesスコアを取得する要素の値。
Method response object
  • Hit
    • Rank: integer
  • Miss
  • Error

特定の情報については、レスポンスオブジェクトを参照してください。

await cacheClient.sortedSetPutElements(
'test-cache',
'test-sorted-set',
new Map<string, number>([
['key1', 10],
['key2', 20],
['key3', 30],
])
);
const result = await cacheClient.sortedSetGetRank('test-cache', 'test-sorted-set', 'key2');
if (result instanceof CacheSortedSetGetRank.Hit) {
console.log(`Element with value 'key1' has rank: ${result.rank()}`);
} else if (result instanceof CacheSortedSetGetRank.Miss) {
console.log("Sorted Set 'test-sorted-set' was not found in cache 'test-cache'");
} else if (result instanceof CacheSortedSetGetRank.Error) {
throw new Error(
`An error occurred while attempting to call cacheSortedSetFetchGetRank on sorted set 'test-sorted-set' in cache 'test-cache': ${result.errorCode()}: ${result.toString()}`
);
}

SortedSetIncrementScore

要素のスコアに加算します。値がソート済みセットに存在しない場合、このメソッドは値をインクリメンタルするように設定します。

注記

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

例:

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

特定の情報については、レスポンスオブジェクトを参照してください。

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

SortedSetElement

ソート済みセット内の各要素は、値とスコアの組み合わせで構成されています。

例: { "TomHocusXaster" : 1138 }

名前説明
ValueString | Bytesソート済みセット要素の値。
ScoreSigned double 64-bit floatソート済みセット要素のスコア。

SortedSetElement は、単独で存在することも、SortedSetElement の配列の一部として存在することもあります。

SortedSetLength

ソート済みセットアイテム内のエントリ数を取得します。

名前説明
cacheNameStringキャッシュの名前
sortedSetNameString確認するソート済みセットアイテムの名前。
Method response object
  • Hit
    • length(): Number
  • Miss
  • Error

特定の情報については、レスポンスオブジェクトを参照してください。

SortedSetLengthByScore

既存のソート済みセットアイテムについて、指定された最小スコアと最大スコアの間のすべての値を検索し、その数を返します。

名前説明
cacheNameStringキャッシュの名前
sortedSetNameString確認するソート済みセットアイテムの名前。
minScoreOptional[double]結果の下限のスコア(を含む)。デフォルトは -inf であり、最低のスコアまで含まれます。
maxScoreOptional[double]結果の上限のスコア(を含む)。デフォルトは +inf であり、最高のスコアまで含まれます。
Method response object
  • Hit
    • length(): Number
  • Miss
  • Error

特定の情報については、レスポンスオブジェクトを参照してください。