Skip to main content

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:

NameTypeDescription
storeNameStringName of the store to be created.

$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

Delete store

Deletes a store

Attributes:

NameTypeDescription
storeNameStringName of the store to be deleted.

$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

List stores

Lists all stores


$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

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.

NameTypeDescription
storeNameStringName of the store.
keyStringThe key under which the value is to be added.
valueString / Bytes / Integer / DoubleThe value to be stored.

$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

Get

Get the value stored for the given key.

NameTypeDescription
storeNameStringName of the store.
keyStringThe key whose value is to be retrieved.

$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

Delete

Delete the value stored for the given key.

NameTypeDescription
storeNameStringName of the store.
keyStringThe key whose value is to be deleted.

$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

Current status of API support in SDKs

For the current status of API support in various SDK languages, see the language support page.