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

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');
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 '${cacheName}'`);
} else if (result instanceof CacheDictionaryFetch.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');
if (result instanceof CacheDictionaryGetField.Hit) {
console.log(
`Field ${result.fieldString()} fetched successfully from cache '${cacheName}' with value: ${result.valueString()}`
);
} else if (result instanceof CacheDictionaryGetField.Miss) {
console.log(`Dictionary 'test-dictionary' was not found in cache '${cacheName}'`);
} else if (result instanceof CacheDictionaryGetField.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']);
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 '${cacheName}'`);
} else if (result instanceof CacheDictionaryGetFields.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);
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 '${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');
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 '${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']);
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 '${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');
if (result instanceof CacheDictionarySetField.Success) {
console.log(`Field set successfully into cache '${cacheName}'`);
} else if (result instanceof CacheDictionarySetField.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'],
])
);
if (result instanceof CacheDictionarySetFields.Success) {
console.log(`Fields set successfully into cache '${cacheName}'`);
} else if (result instanceof CacheDictionarySetFields.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

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