API reference for Momento Storage
Control APIs
These API methods are used to manage and control stores.
Create store
Creates a store with the provided name
Attributes:
Name | Type | Description |
---|---|---|
storeName | String | Name of the store to be created. |
- PHP
- Rust
$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");
}
info
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),
}
info
Full example code and imports can be found here
Delete store
Deletes a store
Attributes:
Name | Type | Description |
---|---|---|
storeName | String | Name of the store to be deleted. |
- PHP
- Rust
$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");
}
info
Full example code and imports can be found here
storage_client.delete_store(store_name).await?;
println!("Store {} deleted", store_name);
info
Full example code and imports can be found here
List stores
Lists all stores
- PHP
- Rust
$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");
}
info
Full example code and imports can be found here
let response = storage_client.list_stores().await?;
println!("Stores: {:#?}", response.stores);
info
Full example code and imports can be found here
Data APIs
These API methods are used to directly interact with data in a store.
Put
Puts a value into the store. If a value for this key is already present it will be replaced by the new value.
Name | Type | Description |
---|---|---|
storeName | String | Name of the store. |
key | String | The key under which the value is to be added. |
value | String / Bytes / Integer / Double | The value to be stored. |
- PHP
- Rust
$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);
info
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);
info
Full example code and imports can be found here
Get
Get the value stored for the given key.
Name | Type | Description |
---|---|---|
storeName | String | Name of the store. |
key | String | The key whose value is to be retrieved. |
- PHP
- Rust
$get_response = $storage_client->get($store_name, "test-key");
if ($found = $get_response->asFound()) {
$value_type = $found->type();
if ($value_type == StorageValueType::STRING) {
print("Got string value: " . $found->valueString() . "\n");
} elseif ($value_type == StorageValueType::INT) {
print("Got integer value: " . $found->valueInt() . "\n");
} elseif ($value_type == StorageValueType::FLOAT) {
print("Got float value: " . $found->valueFloat() . "\n");
} elseif ($value_type == StorageValueType::BYTES) {
// This case is not expected in this example as PHP doesn't have a native byte type
print("Got bytes value: " . $found->valueBytes() . "\n");
}
// You may also pull the value directly from the response without type checking
print("Retrieved value for key 'test-key': {$get_response->value()}\n");
} elseif ($get_response->asNotFound()) {
print("Key 'test-key' was not found in store $store_name\n");
} elseif ($err = $get_response->asError()) {
print("An error occurred while attempting to get key 'test-key' from store $store_name: {$err->errorCode()} - {$err->message()}\n");
}
info
Full example code and imports can be found here
let response = storage_client.get(store_name, "key").await?;
match response {
momento::storage::GetResponse::NotFound => println!("Key not found in {}", store_name),
momento::storage::GetResponse::Found { value } => {
// A Found response indicates the value was found in the store.
// Use `match` to get the value if you don't know the type beforehand:
match value.clone() {
momento::storage::StorageValue::String(value) => println!("Got string value {}", value),
momento::storage::StorageValue::Bytes(value) => println!("Got bytes value {:?}", value),
momento::storage::StorageValue::Integer(value) => println!("Got integer value {}", value),
momento::storage::StorageValue::Double(value) => println!("Got double value {}", value),
}
// If you know the type you're expecting, you can `try_into()` it directly:
let found_value: String = value.try_into()?;
println!("Got value {}", found_value);
}
}
info
Full example code and imports can be found here
Delete
Delete the value stored for the given key.
Name | Type | Description |
---|---|---|
storeName | String | Name of the store. |
key | String | The key whose value is to be deleted. |
- PHP
- Rust
$delete_response = $storage_client->delete($store_name, "test-key");
if ($delete_response->asSuccess()) {
print("Key 'test-key' deleted successfully\n");
} elseif ($err = $delete_response->asError()) {
print("An error occurred while attempting to delete key 'test-key' from store $store_name: {$err->errorCode()} - {$err->message()}\n");
}
info
Full example code and imports can be found here
storage_client.delete(store_name, "key").await?;
println!("Deleted key from store {}", store_name);
info
Full example code and imports can be found here
Current status of API support in SDKs
For the current status of API support in various SDK languages, see the language support page.