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

Momento Cache 用 Dictionary API リファレンス

このページでは、辞書コレクション・データ型 の Momento API メソッドについて詳しく説明します。

備考

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

Dictionary メソッド

DictionaryFetch

キャッシュからDictionary項目を取得します。

NameTypeDescription
cacheNameStringキャッシュの名前
dictionaryNameString取得するDictionary項目の名前
Method response object

DictionaryFetchのレスポンスオブジェクトは、キャッシュヒット、ミス、エラーの3つのオプションを返します。

  • Cache hit
    • valueDictionaryBytesBytes(): Map<Bytes, Bytes>
    • valueDictionaryStringString(): Map<String, String>
    • valueDictionaryStringBytes(): Map<String, Bytes>
    • valueDictionaryBytesString(): Map<Bytes, String>
    • toString(): String - displays the field/value pairs, truncated.
  • Cache miss
  • Cache error

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

await cacheClient.dictionarySetField(cacheName, 'test-dictionary', 'test-field', 'test-value');
const result = await cacheClient.dictionaryFetch(cacheName, 'test-dictionary');

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

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

DictionaryGetField

キャッシュ内のDictionary項目から 1 つのフィールドを取得します。

NameTypeDescription
cacheNameStringキャッシュの名前
dictionaryNameString取得するDictionary項目の名前
fieldString/Bytes取得するDictionary項目のフィールド名
Method response object
  • Cache hit

    • fieldString(): String

    • fieldBytes(): Bytes

    • valueString(): String

    • valueBytes(): Bytes

      これらは、辞書からフィールドとその値を返します。

  • Cache miss

    • fieldString(): String
    • fieldBytes(): Bytes
  • Cache error

    • fieldString(): String
    • fieldBytes(): Bytes

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

await cacheClient.dictionarySetField(cacheName, 'test-dictionary', 'test-field', 'test-value');
const result = await cacheClient.dictionaryGetField(cacheName, 'test-dictionary', 'test-field');

// simplified style; assume the value was found
console.log(`Field 'test-field' fetched from dictionary: ${result.value()!}`);

// pattern-matching style; safer for production code
switch (result.type) {
case CacheDictionaryGetFieldResponse.Hit:
console.log(
`Field ${result.fieldString()} fetched successfully from cache '${cacheName}' with value: ${result.value()}`
);
break;
case CacheDictionaryGetFieldResponse.Miss:
console.log(`Dictionary 'test-dictionary' was not found in cache '${cacheName}'`);
break;
case CacheDictionaryGetFieldResponse.Error:
throw new Error(
`An error occurred while attempting to call cacheDictionaryGetField on dictionary 'test-dictionary' in cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}

DictionaryGetFields

キャッシュ内の辞書から 1 つ以上のフィールドを取得します。

NameTypeDescription
cacheNameString キャッシュの名前
dictionaryNameString 取得するDictionary項目の名前
fieldsString[]/Bytes[]取得するDictionary項目のフィールド名
Method response object
  • Cache hit
    • valueDictionaryBytesBytes(): Map<Bytes, Bytes>
    • valueDictionaryStringString(): Map<String, String>
    • valueDictionaryStringBytes(): Map<String, Bytes>
    • valueDictionaryBytesString(): Map<Bytes, String>
    • toString(): String - displays truncated valueDictionaryStringString()
  • Cache miss
  • Error

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

await cacheClient.dictionarySetFields(
cacheName,
'test-dictionary',
new Map<string, string>([
['key1', 'value1'],
['key2', 'value2'],
])
);
const result = await cacheClient.dictionaryGetFields(cacheName, 'test-dictionary', ['key1', 'key2']);

// simplified style; assume the value was found
console.log(`Got fields from dictionary: ${result.value()!}`);

// pattern-matching style; safer for production code
switch (result.type) {
case CacheDictionaryGetFieldsResponse.Hit:
console.log('Values fetched successfully- ');
result.valueMap().forEach((value, key) => {
console.log(`${key} : ${value}`);
});
break;
case CacheDictionaryGetFieldsResponse.Miss:
console.log(`Dictionary 'test-dictionary' was not found in cache '${cacheName}'`);
break;
case CacheDictionaryGetFieldsResponse.Error:
throw new Error(
`An error occurred while attempting to call cacheDictionaryGetFields on dictionary 'test-dictionary' in cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}

DictionaryIncrement

既存の値が基数 10 の整数を表す UTF-8 文字列である場合にのみ、フィールドの値を追加します。フィールドが辞書にない場合、このメソッドは、フィールドの値をインクリメントする量に設定します。

注記

結果として得られるインクリメント値は、-9223372036854775808 から 9223372036854775807 の間、つまり符号付き 64 ビット整数でなければならりません。そうでない場合、エラー応答が返されます。

Examples:

  • フィールドが存在しない場合、dictionaryIncrement(cache, dict, field, 10) はフィールドの値を 10 に設定する。
  • フィールドが 5 の場合、dictionaryIncrement(cache, dict, field, 10) はフィールドの値を 15 に設定する。
  • field = 'five' のとき、FailedPreconditionException エラーが返される。
NameTypeDescription
cacheNameStringキャッシュの名前
dictionaryNameString取得するDictionary項目の名前
fieldString/Bytes取得するDictionary項目のフィールド名
amountInteger値に加える量。正の値、負の値、またはゼロを指定します。デフォルトは1。
ttlCollectionTTL objectこれはTTLコンストラクトとして戻ってきます
Method response object
  • Success
    • value(): integer - the new value after incrementing
    • toString(): String - displays the value()
  • Error

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

await cacheClient.dictionarySetField(cacheName, 'test-dictionary', 'test-field', '10');
const result = await cacheClient.dictionaryIncrement(cacheName, 'test-dictionary', 'test-field', 1);
switch (result.type) {
case CacheDictionaryIncrementResponse.Success:
console.log(`Field value incremented by 1. Result - ${result.valueNumber()}`);
break;
case CacheDictionaryIncrementResponse.Error:
throw new Error(
`An error occurred while attempting to call cacheDictionaryIncrement on dictionary 'test-dictionary' in cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}

DictionaryRemoveField

Dictionary項目からフィールドを削除する。

NameTypeDescription
cacheNameStringキャッシュの名前
dictionaryNameString取得するDictionary項目の名前
fieldString/Bytes取得するDictionary項目のフィールド名
Method response object
  • Success
  • Error

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

await cacheClient.dictionarySetField(cacheName, 'test-dictionary', 'test-field', '10');
const result = await cacheClient.dictionaryRemoveField(cacheName, 'test-dictionary', 'test-field');
switch (result.type) {
case CacheDictionaryRemoveFieldResponse.Success:
console.log("Field removed successfully from dictionary 'test-dictionary'");
break;
case CacheDictionaryRemoveFieldResponse.Error:
throw new Error(
`An error occurred while attempting to call cacheDictionaryRemoveField on dictionary 'test-dictionary' in cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}

DictionaryRemoveFields

Dictionary項目から複数のフィールドを削除します。

NameTypeDescription
cacheNameStringキャッシュの名前
dictionaryNameString取得するDictionary項目の名前
fieldsString[]/Bytes[]取得するDictionary項目のフィールド名
Method response object
  • Success
  • Error

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

await cacheClient.dictionarySetFields(
cacheName,
'test-dictionary',
new Map<string, string>([
['key1', 'value1'],
['key2', 'value2'],
])
);
const result = await cacheClient.dictionaryRemoveFields(cacheName, 'test-dictionary', ['key1', 'key2']);
switch (result.type) {
case CacheDictionaryRemoveFieldsResponse.Success:
console.log("Fields removed successfully from dictionary 'test-dictionary'");
break;
case CacheDictionaryRemoveFieldsResponse.Error:
throw new Error(
`An error occurred while attempting to call cacheDictionaryRemoveFields on dictionary 'test-dictionary' in cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}

DictionarySetField

既存のDictionary項目のフィールド:値のペアを設定します。辞書項目が存在しない場合は、新しいフィールド:値のペアで作成されます。

NameTypeDescription
cacheNameStringキャッシュの名前
dictionaryNameString設定するDictionary項目名。
fieldString/Bytes設定する辞書項目のフィールド名
valueString/Bytes設定するフィールドの値
ttlCollectionTTL objectキャッシュ内のDictionary項目の TTL。このTTLは、キャッシュ・クライアントを初期化するときに使用されるTTLよりも優先されます。
Method response object
  • Success
  • Error

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

const result = await cacheClient.dictionarySetField(cacheName, 'test-dictionary', 'test-field', 'test-value');
switch (result.type) {
case CacheDictionarySetFieldResponse.Success:
console.log(`Field set successfully into cache '${cacheName}'`);
break;
case CacheDictionarySetFieldResponse.Error:
throw new Error(
`An error occurred while attempting to call cacheDictionarySetField on dictionary 'test-dictionary' in cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}

DictionarySetFields

Dictionary項目に複数のフィールド:値のペアを設定します。辞書項目が存在しない場合は、新しいフィールドで作成されます。

NameTypeDescription
cacheNameStringキャッシュの名前
dictionaryNameString設定するDictionary項目名
fieldsString[]/Bytes[]セット操作によってDictionary項目に追加されるフィールド:値のペア。
ttlCollectionTTL objectキャッシュ内のDictionary項目の TTL。このTTLは、キャッシュ・クライアントを初期化するときに使用されるTTLよりも優先されます。
Method response object
  • Success
  • Error

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

const result = await cacheClient.dictionarySetFields(
cacheName,
'test-dictionary',
new Map<string, string>([
['key1', 'value1'],
['key2', 'value2'],
])
);
switch (result.type) {
case CacheDictionarySetFieldsResponse.Success:
console.log(`Fields set successfully into cache '${cacheName}'`);
break;
case CacheDictionarySetFieldsResponse.Error:
throw new Error(
`An error occurred while attempting to call cacheDictionarySetFields on dictionary 'test-dictionary' in cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}

DictionaryLength

既存のDictionary項目の長さを取得します

NameTypeDescription
cacheNameStringキャッシュの名前
dictionaryNameStringチェックするDictionary項目名
Method response object
  • Hit
    • length(): Number
  • Miss
  • Error

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

  let _length: u32 = cache_client
.dictionary_length(cache_name, "dictionary_name")
.await?
.try_into()
.expect("Expected a dictionary length!");