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

Momento CacheのDictionary APIリファレンス

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

Dictionaryのメソッド

DictionaryFetch

cacheからDictionaryを取得します。

名前説明
cache名前String名前 of the cache.
dictionaryNameString取得するDictionaryの名前。
メソッドのレスポンスオブジェクト

DictionaryFetchで返却されるレスポンスオブジェクトの種類は3つあります。cache hit、miss、errorです。

  • Cache hit
    • valueDictionaryBytesBytes(): Map<Bytes, Bytes>
    • valueDictionaryStringString(): Map<String, String>
    • valueDictionaryStringBytes(): Map<String, Bytes>
    • valueDictionaryBytesString(): Map<Bytes, String>
    • toString(): String - 省略されたfield/valueのペアを表示します。
  • Cache miss
  • Cache error

具体的な情報については、レスポンスオブジェクトを参照してください。

await cacheClient.dictionarySetField('test-cache', 'test-dictionary', 'test-field', 'test-value');
const result = await cacheClient.dictionaryFetch('test-cache', 'test-dictionary');
if (result instanceof CacheDictionaryFetch.Hit) {
console.log('Dictionary fetched successfully- ');
result.valueMapStringString().forEach((value, key) => {
console.log(`${key} : ${value}`);
});
} else if (result instanceof CacheDictionaryFetch.Miss) {
console.log("Dictionary 'test-dictionary' was not found in cache 'test-cache'");
} else if (result instanceof CacheDictionaryFetch.Error) {
throw new Error(
`An error occurred while attempting to call cacheDictionaryFetch on dictionary 'test-dictionary' in cache 'test-cache': ${result.errorCode()}: ${result.toString()}`
);
}

DictionaryGetField

cache内のDictionaryアイテムから1つのfieldを取得します。

名前説明
cacheNameStringCacheの名前。
dictionaryNameString取得するDictionaryの名前。
fieldString/Bytes取得するDictionary内のfieldの名前。
メソッドのレスポンスオブジェクト
  • Cache hit

    • fieldString(): String

    • fieldBytes(): Bytes

    • valueString(): String

    • valueBytes(): Bytes

      これらはfieldとそれに対するvalueをdictionaryから返却します。

  • Cache miss

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

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

具体的な情報については、レスポンスオブジェクトを参照してください。

await cacheClient.dictionarySetField('test-cache', 'test-dictionary', 'test-field', 'test-value');
const result = await cacheClient.dictionaryGetField('test-cache', 'test-dictionary', 'test-field');
if (result instanceof CacheDictionaryGetField.Hit) {
console.log(
`Field ${result.fieldString()} fetched successfully from cache 'test-cache' with value: ${result.valueString()}`
);
} else if (result instanceof CacheDictionaryGetField.Miss) {
console.log("Dictionary 'test-dictionary' was not found in cache 'test-cache'");
} else if (result instanceof CacheDictionaryGetField.Error) {
throw new Error(
`An error occurred while attempting to call cacheDictionaryGetField on dictionary 'test-dictionary' in cache 'test-cache': ${result.errorCode()}: ${result.toString()}`
);
}

DictionaryGetFields

cache内のDictionaryから1つ以上のfieldを取得します。

名前説明
cacheNameStringCacheの名前。
dictionaryNameString取得するDictionaryの名前。
fieldsString[]/Bytes[]取得するDictionary内のfieldの名前。
メソッドのレスポンスオブジェクト
  • Cache hit
    • valueDictionaryBytesBytes(): Map<Bytes, Bytes>
    • valueDictionaryStringString(): Map<String, String>
    • valueDictionaryStringBytes(): Map<String, Bytes>
    • valueDictionaryBytesString(): Map<Bytes, String>
    • toString(): String - valueDictionaryStringString()の省略版を表示します。
  • Cache miss
  • Error

具体的な情報については、レスポンスオブジェクトを参照してください。

await cacheClient.dictionarySetFields(
'test-cache',
'test-dictionary',
new Map<string, string>([
['key1', 'value1'],
['key2', 'value2'],
])
);
const result = await cacheClient.dictionaryGetFields('test-cache', 'test-dictionary', ['key1', 'key2']);
if (result instanceof CacheDictionaryGetFields.Hit) {
console.log('Values fetched successfully- ');
result.valueMapStringString().forEach((value, key) => {
console.log(`${key} : ${value}`);
});
} else if (result instanceof CacheDictionaryGetFields.Miss) {
console.log("Dictionary 'test-dictionary' was not found in cache 'test-cache'");
} else if (result instanceof CacheDictionaryGetFields.Error) {
throw new Error(
`An error occurred while attempting to call cacheDictionaryGetFields on dictionary 'test-dictionary' in cache 'test-cache': ${result.errorCode()}: ${result.toString()}`
);
}

DictionaryIncrement

既存の値が基数10の整数を表すUTF-8文字列である場合に限り、fieldの値に追加します。 fieldがDictionaryにない場合、このメソッドはfieldの値を増分量に設定します。

:::注記

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

:::

例:

  • fieldが存在しない場合、 dictionaryIncrement(cache, dict, field, 10) はfieldの値を10に設定します。
  • field = 5,の場合、 dictionaryIncrement(cache, dict, field, 10)はfieldの値を15に設定します。
  • field = ‘five’、FailedPreconditionException エラーで応答します。
名前説明
cacheNameStringCacheの名前。
dictionaryNameString取得するDictionaryの名前。
fieldString/Bytes取得するDictionary内のfieldの名前。
amountInteger値に追加する数量。 正、負、またはゼロの場合があります。 デフォルトは 1 です。
ttlCollectionTTL objectこれはTTLコンストラクトとして返されます。
メソッドのレスポンスオブジェクト
  • Success
    • value(): integer - 追加が行われた後の新しい値
    • toString(): String - value()を表示します
  • Error

具体的な情報については、レスポンスオブジェクトを参照してください。

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

DictionaryRemoveField

Dictionaryアイテムからfieldを削除します。

名前説明
cacheNameStringCacheの名前。
dictionaryNameString取得するDictionaryの名前。
fieldString/Bytes取得するDictionary内のfieldの名前。
メソッドのレスポンスオブジェクト
  • Success
  • Error

具体的な情報については、レスポンスオブジェクトを参照してください。

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

DictionaryRemoveFields

Dictionaryアイテムから複数のfieldを削除します。

名前説明
cacheNameStringCacheの名前。
dictionaryNameString取得するDictionaryの名前。
fieldsString[]/Bytes[]取得するDictionary内のfieldの名前。
メソッドのレスポンスオブジェクト
  • Success
  • Error

具体的な情報については、レスポンスオブジェクトを参照してください。

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

DictionarySetField

既存のDictionaryにfield:value ペアを設定します。 Dictionaryが存在しない場合は、新しいfield:valueのペアで作成されます。

名前説明
cacheNameStringCacheの名前。
dictionaryNameStringDictionaryの名前。
fieldString/Bytes設定するDictionaryのfieldの名前。
valueString/Bytes設定するfieldの値。
ttlCollectionTTL objectCache内のDictionaryのTTL。このTTL はCacheクライアントの初期化時に使用されるTTLよりも優先されます。
メソッドのレスポンスオブジェクト
  • Success
  • Error

具体的な情報については、レスポンスオブジェクトを参照してください。

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

DictionarySetFields

Dictionaryに複数のfield:valueのペアを設定します。 Dictionaryアイテムが存在しない場合は、新しいfieldを使用して作成されます。

名前説明
cacheNameStringCacheの名前。
dictionaryNameStringDictionaryの名前。
fieldsString[]/Bytes[]設定操作によってディクショナリ項目に追加されるfield:valueのペア。
ttlCollectionTTL objectCache内のDictionaryのTTL。このTTLはCacheクライアントの初期化時に使用されるTTLよりも優先されます。
メソッドのレスポンスオブジェクト
  • Success
  • Error

具体的な情報については、レスポンスオブジェクトを参照してください。

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

DictionaryLength

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

名前説明
cacheNameStringCacheの名前。
dictionaryNameStringDictionaryの名前。
メソッドのレスポンスオブジェクト
  • Hit
    • length(): Number
  • Miss
  • Error

具体的な情報については、レスポンスオブジェクトを参照してください。