Momento StorageのAPIリファレンス
Control APIs
これらのAPIメソッドは、ストアの管理と制御に使用されます。
ストアの作成
指定された名前のストアを作成します。
属性:
Name | Type | Description |
---|---|---|
storeName | String | 作成されるストアの名前。 |
- JavaScript
- Java
- Go
- PHP
- Rust
const result = await storageClient.createStore(storeName);
switch (result.type) {
case CreateStoreResponse.AlreadyExists:
console.log(`Store '${storeName}' already exists`);
break;
case CreateStoreResponse.Success:
console.log(`Store '${storeName}' created`);
break;
case CreateStoreResponse.Error:
throw new Error(
`An error occurred while attempting to create store '${storeName}': ${result.errorCode()}: ${result.toString()}`
);
}
備考
Full example code and imports can be found here
final CreateStoreResponse response = storageClient.createStore("test-store").join();
if (response instanceof CreateStoreResponse.Success) {
System.out.println("Store 'test-store' created");
} else if (response instanceof CreateStoreResponse.Error error) {
if (error.getCause() instanceof StoreAlreadyExistsException) {
System.out.println("Store 'test-store' already exists");
} else {
throw new RuntimeException(
"An error occurred while attempting to create store 'test-store': "
+ error.getErrorCode(),
error);
}
}
備考
Full example code and imports can be found here
resp, err := storageClient.CreateStore(ctx, &momento.CreateStoreRequest{
StoreName: storeName,
})
if err != nil {
panic(err)
}
switch resp.(type) {
case *responses.CreateStoreSuccess:
fmt.Printf("Successfully created store %s\n", storeName)
case *responses.CreateStoreAlreadyExists:
fmt.Printf("Store %s already exists\n", storeName)
}
備考
Full example code and imports can be found here
$create_store_response = $storage_client->createStore($store_name);
if ($create_store_response->asSuccess()) {
print("Store $store_name created\n");
} elseif ($create_store_response->asAlreadyExists()) {
print("Store $store_name already exists\n");
} elseif ($err = $create_store_response->asError()) {
print("An error occurred while attempting to create $store_name: {$err->errorCode()} - {$err->message()}\n");
}
備考
Full example code and imports can be found here
let response = storage_client.create_store(store_name).await?;
match response {
momento::storage::CreateStoreResponse::Created => println!("Store {} created", store_name),
momento::storage::CreateStoreResponse::AlreadyExists => println!("Store {} already exists", store_name),
}
備考
Full example code and imports can be found here
ストアの削除
ストアを削除します。
属性:
Name | Type | Description |
---|---|---|
storeName | String | 削除するストアの名前。 |
- JavaScript
- Java
- Go
- PHP
- Rust
const result = await storageClient.deleteStore(storeName);
switch (result.type) {
case DeleteStoreResponse.Success:
console.log(`Store '${storeName}' deleted`);
break;
case DeleteStoreResponse.Error:
throw new Error(
`An error occurred while attempting to delete store '${storeName}': ${result.errorCode()}: ${result.toString()}`
);
}
備考
Full example code and imports can be found here
final DeleteStoreResponse response = storageClient.deleteStore("test-store").join();
if (response instanceof DeleteStoreResponse.Success) {
System.out.println("Store 'test-store' deleted");
} else if (response instanceof DeleteStoreResponse.Error error) {
throw new RuntimeException(
"An error occurred while attempting to delete store 'test-store': "
+ error.getErrorCode(),
error);
}
備考
Full example code and imports can be found here
_, err := storageClient.DeleteStore(ctx, &momento.DeleteStoreRequest{
StoreName: storeName,
})
if err != nil {
panic(err)
}
備考
Full example code and imports can be found here
$delete_store_response = $storage_client->deleteStore($store_name);
if ($err = $delete_store_response->asError()) {
print("An error occurred while attempting to delete $store_name: {$err->errorCode()} - {$err->message()}\n");
} else {
print("Store $store_name deleted\n");
}
備考
Full example code and imports can be found here
storage_client.delete_store(store_name).await?;
println!("Store {} deleted", store_name);
備考
Full example code and imports can be found here
ストアのリスト
全てのストアをリストします
- JavaScript
- Java
- Go
- PHP
- Rust
const result = await storageClient.listStores();
switch (result.type) {
case ListStoresResponse.Success:
console.log(
`Stores:\n${result
.stores()
.map(c => c.getName())
.join('\n')}\n\n`
);
break;
case ListStoresResponse.Error:
throw new Error(`An error occurred while attempting to list stores: ${result.errorCode()}: ${result.toString()}`);
}
備考
Full example code and imports can be found here
final ListStoresResponse response = storageClient.listStores().join();
if (response instanceof ListStoresResponse.Success success) {
final String stores =
success.getStores().stream().map(StoreInfo::getName).collect(Collectors.joining("\n"));
System.out.println("Stores:\n" + stores);
} else if (response instanceof ListStoresResponse.Error error) {
throw new RuntimeException(
"An error occurred while attempting to list stores: " + error.getErrorCode(), error);
}
備考
Full example code and imports can be found here
resp, err := storageClient.ListStores(ctx, &momento.ListStoresRequest{})
if err != nil {
panic(err)
}
switch r := resp.(type) {
case *responses.ListStoresSuccess:
log.Printf("Found stores:\n")
for _, store := range r.Stores() {
log.Printf("\tStore name: %s\n", store.Name())
}
}
備考
Full example code and imports can be found here
$list_stores_response = $storage_client->listStores();
if ($listSuccess = $list_stores_response->asSuccess()) {
print("Found stores:\n");
foreach ($listSuccess->stores() as $store) {
$store_name = $store->name();
print("- $store_name\n");
}
} elseif ($err = $list_stores_response->asError()) {
print("An error occurred while attempting to list stores: {$err->errorCode()} - {$err->message()}\n");
}
備考
Full example code and imports can be found here
let response = storage_client.list_stores().await?;
println!("Stores: {:#?}", response.stores);
備考
Full example code and imports can be found here
Data APIs
これらのAPIメソッドは、ストア内のデータを直接操作するために使用されます。
Put
値をストアに入れる。このキーの値がすでに存在する場合は、新しい値に置き換えられます。
Name | Type | Description |
---|---|---|
storeName | String | ストアの名前 |
key | String | 値を追加するキー |
value | String / Bytes / Integer / Double | 保存する値 |
- JavaScript
- Java
- Go
- PHP
- Rust
// to store a string value:
const result = await storageClient.putString(storeName, 'test-key', 'test-value');
switch (result.type) {
case StoragePutResponse.Success:
console.log("Key 'test-key' stored successfully");
break;
case StoragePutResponse.Error:
throw new Error(
`An error occurred while attempting to store key 'test-key' in store '${storeName}': ${result.errorCode()}: ${result.toString()}`
);
}
// Momento storage also supports these other data types:
await storageClient.putInt(storeName, 'test-key', 42);
await storageClient.putDouble(storeName, 'test-key', 3.14);
await storageClient.putBytes(storeName, 'test-key', Buffer.from('test-value'));
備考
Full example code and imports can be found here
// this example illustrates how to store a String value
final PutResponse response = storageClient.put("test-store", "test-key", "test-value").join();
if (response instanceof PutResponse.Success) {
System.out.println("Key 'test-key' stored successfully");
} else if (response instanceof PutResponse.Error error) {
throw new RuntimeException(
"An error occurred while attempting to store key 'test-key' in store 'test-store': "
+ error.getErrorCode(),
error);
}
// Momento Storage also supports storing values of type byte[], long, and double:
byte[] bytesValue = "test-byte-array-value".getBytes(StandardCharsets.UTF_8);
storageClient.put("test-store", "test-byte-array-key", bytesValue).join();
storageClient.put("test-store", "test-integer-key", 42L).join();
storageClient.put("test-store", "test-double-key", 42.0).join();
備考
Full example code and imports can be found here
_, err := storageClient.Put(ctx, &momento.StoragePutRequest{
StoreName: storeName,
Key: "key",
Value: storageTypes.String("my-value"),
})
if err != nil {
panic(err)
}
// Momento storage also supports these other data types:
storageClient.Put(ctx, &momento.StoragePutRequest{
StoreName: storeName,
Key: "key",
Value: storageTypes.Int(42),
})
storageClient.Put(ctx, &momento.StoragePutRequest{
StoreName: storeName,
Key: "key",
Value: storageTypes.Float(3.14),
})
storageClient.Put(ctx, &momento.StoragePutRequest{
StoreName: storeName,
Key: "key",
Value: storageTypes.Bytes{0x01, 0x02, 0x03},
})
備考
Full example code and imports can be found here
$put_response = $storage_client->putString($store_name, "test-key", "test-value");
if ($put_response->asSuccess()) {
print("Key 'test-key' stored successfully\n");
} elseif ($err = $put_response->asError()) {
print("An error occurred while attempting to store 'test-key': {$err->errorCode()} - {$err->message()}\n");
}
// Momento storage also supports int, float, and bytes types.
// Because strings in PHP are a series of bytes, the putBytes method accepts a string as the value.
$put_response = $storage_client->putBytes($store_name, "test-key", "test-value");
$put_response = $storage_client->putInt($store_name, "test-key", 42);
$put_response = $storage_client->putFloat($store_name, "test-key", 3.14);
備考
Full example code and imports can be found here
storage_client.put(store_name, "key", "value").await?;
println!("Put key and value in store {}", store_name);
備考
Full example code and imports can be found here
Get
与えられたキーに対して格納されている値を取得します。
Name | Type | Description |
---|---|---|
storeName | String | ストアの名前 |
key | String | 値を取得するキー |
- JavaScript
- Java
- Go
- PHP
- Rust
const getResponse = await storageClient.get(storeName, 'test-key');
// simplified style; assume the value was found, and that it was a string
console.log(`string hit: ${getResponse.value()!.string()!}`);
// if the value was an integer:
const integerGetResponse = await storageClient.get(storeName, 'test-integer-key');
console.log(`integer hit: ${integerGetResponse.value()!.int()!}`);
// pattern-matching style; safer for production code
switch (getResponse.type) {
case StorageGetResponse.Found:
// if you know the value is a string:
console.log(`Retrieved value for key 'test-key': ${getResponse.value().string()!}`);
break;
case StorageGetResponse.NotFound:
console.log(`Key 'test-key' was not found in store '${storeName}'`);
break;
case StorageGetResponse.Error:
throw new Error(
`An error occurred while attempting to get key 'test-key' from store '${storeName}': ${getResponse.errorCode()}: ${getResponse.toString()}`
);
}