Momento Cache 用 Dictionary API リファレンス
このページでは、辞書コレクション・データ型 の Momento API メソッドについて詳しく説明します。
Momentoコレクションタイプは、CollectionTTLを使用してTTL動作を指定します。これは、すべての "write" 操作のオプション引数です。
Dictionary メソッド
DictionaryFetch
キャッシュからDictionary項目を取得します。
Name | Type | Description |
---|---|---|
cacheName | String | キャッシュの名前 |
dictionaryName | String | 取得する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
詳しくはレスポンスオブジェクトを参照してください。
- JavaScript
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 つのフィールドを取得します。
Name | Type | Description |
---|---|---|
cacheName | String | キャッシュの名前 |
dictionaryName | String | 取得するDictionary項目の名前 |
field | String/Bytes | 取得するDictionary項目のフィールド名 |
Method response object
Cache hit
fieldString()
: StringfieldBytes()
: BytesvalueString()
: StringvalueBytes()
: Bytesこれらは、辞書からフィールドとその値を返します。
Cache miss
fieldString()
: StringfieldBytes()
: Bytes
Cache error
fieldString()
: StringfieldBytes()
: Bytes
詳しくはレスポンスオブジェクトを参照。
- JavaScript
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 つ以上のフィールドを取得します。
Name | Type | Description |
---|---|---|
cacheName | String | キャッシュの名前 |
dictionaryName | String | 取得するDictionary項目の名前 |
fields | String[]/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
詳しくはレスポンスオブジェクトを参照。
- JavaScript
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 エラーが返される。
Name | Type | Description |
---|---|---|
cacheName | String | キャッシュの名前 |
dictionaryName | String | 取得するDictionary項目の名前 |
field | String/Bytes | 取得するDictionary項目のフィールド名 |
amount | Integer | 値に加える量。正の値、負の値、またはゼロを指定します。デフォルトは1。 |
ttl | CollectionTTL object | これはTTLコンストラクトとして戻ってきます |
Method response object
- Success
value()
: integer - the new value after incrementingtoString()
: String - displays the value()
- Error
詳しくはレスポンスオブジェクトを参照。
- JavaScript
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項目からフィールドを削除する。
Name | Type | Description |
---|---|---|
cacheName | String | キャッシュの名前 |
dictionaryName | String | 取得するDictionary項目の名前 |
field | String/Bytes | 取得するDictionary項目のフィールド名 |
Method response object
- Success
- Error
詳しくはレスポンスオブジェクトを参照。
- JavaScript
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項目から複数のフィールドを削除します。
Name | Type | Description |
---|---|---|
cacheName | String | キャッシュの名前 |
dictionaryName | String | 取得するDictionary項目の名前 |
fields | String[]/Bytes[] | 取得するDictionary項目のフィールド名 |
Method response object
- Success
- Error
詳しくはレスポンスオブジェクトを参照。
- JavaScript
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項目のフィールド:値のペアを設定します。辞書項目が存在しない場合は、新しいフィールド:値のペアで作成されます。
Name | Type | Description |
---|---|---|
cacheName | String | キャッシュの名前 |
dictionaryName | String | 設定するDictionary項目名。 |
field | String/Bytes | 設定する辞書項目のフィールド名 |
value | String/Bytes | 設定するフィールドの値 |
ttl | CollectionTTL object | キャッシュ内のDictionary項目の TTL。このTTLは、キャッシュ・クライアントを初期化するときに使用されるTTLよりも優先されます。 |
Method response object
- Success
- Error
詳しくはレスポンスオブジェクトを参照。
- JavaScript
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項目に複数のフィールド:値のペアを設定します。辞書項目が存在しない場合は、新しいフィールドで作成されます。
Name | Type | Description |
---|---|---|
cacheName | String | キャッシュの名前 |
dictionaryName | String | 設定するDictionary項目名 |
fields | String[]/Bytes[] | セット操作によってDictionary項目に追加されるフィールド:値のペア。 |
ttl | CollectionTTL object | キャッシュ内のDictionary項目の TTL。このTTLは、キャッシュ・クライアントを初期化するときに使用されるTTLよりも優先されます。 |
Method response object
- Success
- Error
詳しくはレスポンスオブジェクトを参照。
- JavaScript
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項目の長さを取得します
Name | Type | Description |
---|---|---|
cacheName | String | キャッシュの名前 |
dictionaryName | String | チェックするDictionary項目名 |
Method response object
- Hit
length()
: Number
- Miss
- Error
詳しくはレスポンスオブジェクトを参照。