Sorted set collections
Momento Cacheのソート済みセットは、値(String、Byte[]など)とスコア(符号付きダブル64ビットfloat)のペアを持つ一意の要素のコレクションです。ソートされたセットの要素は、スコア順に並べられます。
Momento コレクションタイプは、CollectionTTLを使用してTTL動作を指定します。これは、すべての "write" 操作のオプション引数です。
Sorted set methods
SortedSetPutElement
ソートされたセットに新しい要素を追加したり、既存のソートされたセット要素を更新したりします。
セットが存在しない場合、このメソッドは渡された要素を持つ新しいソート済みセットコレクションを作成します。
セットが存在する場合、その 値 が存在しなければ、その要素はソートされたセットに追加されます。その要素の値が存在する場合、その要素は上書きされます。
Name | Type | Description |
---|---|---|
cacheName | String | キャッシュの名前 |
setName | String | 変更するソートセットコレクションの名前。 |
value | String | Byte[] | The value of the element to be added to the sorted set by this operation. |
score | number | The score of the element to be added to the sorted set by this operation. |
ttl | CollectionTTL object | TTL for the sorted set collection. This TTL takes precedence over the TTL used when initializing a cache connection client. |
Method response object
- Success
- Error
詳しくはレスポンスオブジェクトを参照してください。
- JavaScript
- Elixir
const result = await cacheClient.sortedSetPutElement(cacheName, '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 '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}
case Momento.CacheClient.sorted_set_put_element(
client,
"test-cache",
"test-sorted-set",
"test-value",
5.0
) do
{:ok, _} ->
IO.puts(
"Value 'test-value' with score '5' added successfully to sorted set 'test-sorted-set'"
)
{:error, error} ->
IO.puts(
"An error occurred while attempting to put an element into sorted set 'test-sorted-set' in cache 'test-cache': #{error.error_code}"
)
raise error
end
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.
Name | Type | Description |
---|---|---|
cacheName | String | キャッシュの名前 |
setName | String | 変更するソートセットコレクションの名前。 |
elements | SortedSetElement[] | この操作によってソートされたセットに追加される要素。 |
ttl | CollectionTTL object | ソートされたセットコレクションの TTL。この TTL は、キャッシュ接続クライアントを初期化するときに使用される TTL よりも優先されます。 |
Method response object
- Success
- Error
詳しくはレスポンスオブジェクトを参照してください。
- JavaScript
- Elixir
const result = await cacheClient.sortedSetPutElements(
cacheName,
'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 '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}
case Momento.CacheClient.sorted_set_put_elements(client, "test-cache", "test-sorted-set", [
{"key1", 10.0},
{"key2", 20.0}
]) do
{:ok, _} ->
IO.puts("Elements added successfully to sorted set 'test-sorted-set'")
{:error, error} ->
IO.puts(
"An error occurred while attempting to put elements into sorted set 'test-sorted-set' in cache 'test-cache': #{error.error_code}"
)
raise error
end
SortedSetFetchByRank
ソートされた集合の要素を取得し、オプションで順位でフィルタリングして、昇順または降順で返します。
Name | Type | Description |
---|---|---|
cacheName | String | キャッシュの名前 |
setName | String | ソートされたセットコレクションの名前。 |
startRank | Optional[integer] | 結果の開始順位。デフォルトはゼロです。 |
endRank | Optional[integer] | 結果の排他的な終了順位。デフォルトは null である。 |
order | Ascending | Descending | ソートされたセットを返したい順番。 |
Method response object
- Hit
- elements(): SortedSetElement[]
- Miss
- Error
詳しくはレスポンスオブジェクトを参照してください。
- JavaScript
- Elixir
await cacheClient.sortedSetPutElements(
cacheName,
'test-sorted-set',
new Map<string, number>([
['key1', 10],
['key2', 20],
])
);
const result = await cacheClient.sortedSetFetchByRank(cacheName, '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 '${cacheName}'`);
} 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 '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}
{:ok, _} =
Momento.CacheClient.sorted_set_put_elements(client, "test-cache", "test-sorted-set", [
{"key1", 10.0},
{"key2", 20.0}
])
case Momento.CacheClient.sorted_set_fetch_by_rank(client, "test-cache", "test-sorted-set") do
{:ok, hit} ->
IO.puts("Values from sorted set 'test-sorted-set' fetched by rank successfully:")
IO.inspect(hit.value)
:miss ->
IO.puts("Sorted Set 'test-sorted-set' was not found in cache 'test-cache'")
{:error, error} ->
IO.puts(
"An error occurred while attempting to fetch by rank on sorted set 'test-sorted-set' in cache 'test-cache': #{error.error_code}"
)
raise error
end
SortedSetFetchByScore
ソートされた集合の要素を取得し、スコアでフィルタリングして昇順または降順で返します。
Name | Type | Description |
---|---|---|
cacheName | String | キャッシュの名前 |
setName | String | ソートされたセットコレクションの名前。 |
minScore | Optional[double] | 結果の低スコアを含む。デフォルトは -inf で、最低スコアまで含める。 |
maxScore | Optional[double] | 結果のハイスコア。デフォルトは +inf である。 |
order | Ascending | Descending | ソートされたセットを返したい順番。 |
offset | Optional[int] | フィルタリングされたリストから結果を返し始めるオフセット。デフォルトは0、つまりフィルタリングしない。指定する場合は、非負でなければならない。 |
count | Optional[int] | フィルタリングされたリストから返す結果の最大数。デフォルトは null で、つまり無制限である。指定する場合は、厳密に正数でなければならない。 |
Method response object
- Hit
- elements(): SortedSetElement[]
- Miss
- Error
詳しくはレスポンスオブジェクトを参照してください。
- JavaScript
- Elixir
await cacheClient.sortedSetPutElements(
cacheName,
'test-sorted-set',
new Map<string, number>([
['key1', 100],
['key2', 25],
])
);
const result = await cacheClient.sortedSetFetchByScore(cacheName, '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 '${cacheName}'`);
} 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 '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}
{:ok, _} =
Momento.CacheClient.sorted_set_put_elements(client, "test-cache", "test-sorted-set", [
{"key1", 10.0},
{"key2", 20.0}
])
case Momento.CacheClient.sorted_set_fetch_by_score(client, "test-cache", "test-sorted-set") do
{:ok, hit} ->
IO.puts("Values from sorted set 'test-sorted-set' fetched by score successfully:")
IO.inspect(hit.value)
:miss ->
IO.puts("Sorted Set 'test-sorted-set' was not found in cache 'test-cache'")
{:error, error} ->
IO.puts(
"An error occurred while attempting to fetch by score on sorted set 'test-sorted-set' in cache 'test-cache': #{error.error_code}"
)
raise error
end
SortedSetGetScore
Gets an element's score from the sorted set, indexed by value.
Name | Type | Description |
---|---|---|
cacheName | String | キャッシュの名前 |
setName | String | ソートされたセットコレクションの名前。 |
value | String | Bytes | スコアを取得する値。 |
Method response object
- Cache hit
- Score: number
- Cache miss (if the sorted set does not exist)
- Error
詳しくはレスポンスオブジェクトを参照してください。
- JavaScript
- Elixir
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');
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 '${cacheName}'`);
} 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 '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}
{:ok, _} =
Momento.CacheClient.sorted_set_put_elements(client, "test-cache", "test-sorted-set", [
{"key1", 10.0},
{"key2", 20.0}
])
case Momento.CacheClient.sorted_set_get_score(client, "test-cache", "test-sorted-set", "key1") do
{:ok, hit} ->
IO.puts("Element with value 'key1' has score: #{hit.score}")
:miss ->
IO.puts(
"Value 'key1' not found in sorted set, or sorted set 'test-sorted-set' was not found in cache 'test-cache'"
)
{:error, error} ->
IO.puts(
"An error occurred while attempting to get the score of 'key1' in sorted set 'test-sorted-set' in cache 'test-cache': #{error.error_code}"
)
raise error
end
SortedSetGetScores
ソートされたセットから、値でインデックス付けされた要素のリストに関連付けられたスコアを取得します。
Name | Type | Description |
---|---|---|
cacheName | String | キャッシュの名前 |
setName | String | ソートされたセットコレクションの名前。 |
values | String[] | Bytes[] | スコアを取得する値の配列。 |
Method response object
- Cache hit
- Elements() (returns hit/miss per element)
- Hit:
- Score: number
- Miss
- Hit:
- Elements() (returns hit/miss per element)
- Cache miss (if the sorted set does not exist)
- Error
詳しくはレスポンスオブジェクトを参照してください。
- JavaScript
- Elixir
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']);
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 '${cacheName}'`);
} 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 '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}
{:ok, _} =
Momento.CacheClient.sorted_set_put_elements(client, "test-cache", "test-sorted-set", [
{"key1", 10.0},
{"key2", 20.0}
])
case Momento.CacheClient.sorted_set_get_scores(client, "test-cache", "test-sorted-set", [
"key1",
"key2"
]) do
{:ok, hit} ->
IO.puts("Element scores retrieved successfully:")
IO.inspect(hit.value)
:miss ->
IO.puts("Sorted Set 'test-sorted-set' was not found in cache 'test-cache'")
{:error, error} ->
IO.puts(
"An error occurred while attempting to get the scores of values in sorted set 'test-sorted-set' in cache 'test-cache': #{error.error_code}"
)
raise error
end
SortedSetRemoveElement
値でインデックス付けされた、ソートされたセットから要素を削除します。
Name | Type | Description |
---|---|---|
cacheName | String | キャッシュの名前 |
setName | String | 変更するセットコレクションの名前。 |
value | String | Bytes | この操作によって削除される要素の値。 |
Method response object
- Success
- Error
詳しくはレスポンスオブジェクトを参照してください。
- JavaScript
- Elixir
await cacheClient.sortedSetPutElement(cacheName, 'test-sorted-set', 'test-value', 10);
const result = await cacheClient.sortedSetRemoveElement(cacheName, '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 '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}
{:ok, _} =
Momento.CacheClient.sorted_set_put_element(
client,
"test-cache",
"test-sorted-set",
"key1",
10.0
)
case Momento.CacheClient.sorted_set_remove_element(
client,
"test-cache",
"test-sorted-set",
"key1"
) do
{:ok, _} ->
IO.puts("Element with value 'key1' removed successfully")
{:error, error} ->
IO.puts(
"An error occurred while attempting to remove value 'key1' from sorted set 'test-sorted-set' in cache 'test-cache': #{error.error_code}"
)
raise error
end
SortedSetRemoveElements
値でインデックス付けされたソートされたセットから要素を削除します。
Name | Type | Description |
---|---|---|
cacheName | String | キャッシュの名前 |
setName | String | 変更するセットコレクションの名前。 |
values | String[] | Bytes[] | この操作によって削除される要素の値。 |
You can remove either one or a specific group of elements.
Method response object
- Success
- Error
詳しくはレスポンスオブジェクトを参照してください。
- JavaScript
- Elixir
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']);
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 '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}
{:ok, _} =
Momento.CacheClient.sorted_set_put_elements(client, "test-cache", "test-sorted-set", [
{"key1", 10.0},
{"key2", 20.0}
])
case Momento.CacheClient.sorted_set_remove_elements(client, "test-cache", "test-sorted-set", [
"key1",
"key2"
]) do
{:ok, _} ->
IO.puts("Elements with value 'key1' and 'key2' removed successfully")
{:error, error} ->
IO.puts(
"An error occurred while attempting remove values from sorted set 'test-sorted-set' in cache 'test-cache': #{error.error_code}"
)
raise error
end
SortedSetGetRank
指定されたソートされた集合の中で、要素はどの位置にあるか?が分かります。
Name | Type | Description |
---|---|---|
cacheName | String | キャッシュの名前 |
setName | String | 変更するソートセットコレクションの名前。 |
value | String | Bytes | スコアを取得する要素の値。 |
Method response object
- Hit
- Rank: integer
- Miss
- Error
詳しくはレスポンスオブジェクトを参照してください。
- JavaScript
- Elixir
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');
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 '${cacheName}'`);
} 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 '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}
{:ok, _} =
Momento.CacheClient.sorted_set_put_elements(client, "test-cache", "test-sorted-set", [
{"key1", 10.0},
{"key2", 20.0}
])
case Momento.CacheClient.sorted_set_get_rank(client, "test-cache", "test-sorted-set", "key1") do
{:ok, hit} ->
IO.puts("Element with value 'key1' has rank: #{hit.rank}")
:miss ->
IO.puts(
"Value 'key1' not found in sorted set, or sorted set 'test-sorted-set' was not found in cache 'test-cache'"
)
{:error, error} ->
IO.puts(
"An error occurred while attempting to get the rank of 'key1' in sorted set 'test-sorted-set' in cache 'test-cache': #{error.error_code}"
)
raise error
end
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に設定します。
Name | Type | Description |
---|---|---|
cacheName | String | キャッシュの名前 |
setName | String | 変更するソートセットコレクションの名前。 |
value | String | Bytes | この操作によってインクリメントされる要素の値。 |
amount | Number | スコアに加算する量。正、負、ゼロのいずれかを指定する。デフォルトは1。 |
ttl | CollectionTTL object | ソートされたセット・コレクションの TTL。この TTL は、キャッシュ接続クライアントを初期化するときに使用される TTL よりも優先されます。 |
Method response object
- Success
- Value: number - the new value after incrementing
- Error
詳しくはレスポンスオブジェクトを参照してください。
- JavaScript
- Elixir
await cacheClient.sortedSetPutElement(cacheName, 'test-sorted-set', 'test-value', 10);
const result = await cacheClient.sortedSetIncrementScore(cacheName, '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 '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}
case Momento.CacheClient.sorted_set_increment_score(
client,
"test-cache",
"test-sorted-set",
"key1",
1
) do
{:ok, result} ->
IO.puts("Score for value 'key1' incremented successfully. New score: #{result.score}")
{:error, error} ->
IO.puts(
"An error occurred while attempting to increment the score of 'key1' in sorted set 'test-sorted-set' in cache 'test-cache': #{error.error_code}"
)
raise error
end
SortedSetElement
ソートされた集合の各要素を構成するのは、値とスコアです。
例:
{ "TomHocusXaster" : 1138 }
Name | Type | Description |
---|---|---|
Value | String | Bytes | ソートされたセット要素の値。 |
Score | Signed double 64-bit float | 要素に得点をつける。 |
SortedSetElementは、それ自体で存在することもできるし、SortedSetElementの配列の一部として存在することもできます。
SortedSetLength
ソートされたセットコレクションのエントリ数を取得します。
Name | Type | Description |
---|---|---|
cacheName | String | キャッシュの名前 |
sortedSetName | String | チェックするソートセットコレクションの名前。 |
Method response object
- Hit
length()
: Number
- Miss
- Error
詳しくはレスポンスオブジェクトを参照してください。
SortedSetLengthByScore
既存のソートされた集合コレクションに対して、指定された最小スコアと最大スコアの間のすべての値を見つけ、その長さを返します。
Name | Type | Description |
---|---|---|
cacheName | String | キャッシュの名前 |
sortedSetName | String | チェックするソートセットコレクションの名前。 |
minScore | Optional[double] | 結果の低スコアを含む。デフォルトは -inf で、最低スコアまで含める。 |
maxScore | Optional[double] | 結果のハイスコア。デフォルトは +inf である。 |
Method response object
- Hit
length()
: Number
- Miss
- Error
詳しくはレスポンスオブジェクトを参照してください。