API reference for Momento Cache
Control APIs
These API methods are used to manage and control caches.
Create cache
Creates a cache with the provided name
Attributes:
Name | Type | Description |
---|---|---|
cacheName | String | Name of the cache to be created. |
- JavaScript
- Python
- Java
- Go
- C#
- Rust
- Ruby
- CLI
const authToken="eyJhbGc.MyTestToken";
const defaultTTL = 15;
const momento = new SimpleCacheClient(authToken, defaultTtl);
momento.createCache('test-cache');
import momento.simple_cache_client as scc
_MOMENTO_AUTH_TOKEN = "eyJhbGc.MyTestToken"
_ITEM_DEFAULT_TTL_SECONDS = 15
with scc.SimpleCacheClient(_MOMENTO_AUTH_TOKEN, _ITEM_DEFAULT_TTL_SECONDS) as cache_client:
cache_client.create_cache('test-cache')
String MOMENTO_AUTH_TOKEN = "eyJhbGc.MyTestToken";
int DEFAULT_ITEM_TTL_SECONDS = 15;
SimpleCacheClient simpleCacheClient = SimpleCacheClient
.builder(MOMENTO_AUTH_TOKEN, DEFAULT_ITEM_TTL_SECONDS)
.build()
simpleCacheClient.createCache("test-cache");
const (
authToken = "eyJhbGc.MyTestToken"
itemDefaultTtlSeconds = 15
)
client, err := momento.NewSimpleCacheClient(authToken, itemDefaultTtlSeconds)
if err != nil {
panic(err)
}
err = client.CreateCache(&momento.CreateCacheRequest{
CacheName: "test-cache",
})
TimeSpan DEFAULT_TTL = TimeSpan.FromSeconds(15);
ICredentialProvider authProvider = new StringMomentoTokenProvider("eyJhbGc.MyTestToken");
using SimpleCacheClient client = new SimpleCacheClient(Configurations.Laptop.Latest(), authProvider, DEFAULT_TTL);
await client.CreateCacheAsync("test-cache");
let auth_token = "eyJhbGc.MyTestToken";
let item_default_ttl_seconds = 15;
let mut cache_client = SimpleCacheClientBuilder::new(
auth_token,
NonZeroU64::new(item_default_ttl_seconds).unwrap(),
)
.unwrap()
.build();
let cache_name = String::from("test-cache");
match cache_client.create_cache(&cache_name).await {
Ok(_) => {}
Err(err) => {
eprintln!("{}", err);
}
}
require 'momento'
MOMENTO_AUTH_TOKEN = "eyJhbGc.MyTestToken"
DEFAULT_TTL_SECONDS = 15
client = Momento::SimpleCacheClient.new(
auth_token: MOMENTO_AUTH_TOKEN, default_ttl: DEFAULT_TTL_SECONDS
)
response = client.create_cache('test-cache')
raise repsonse.error if response.error?
momento cache create --name test-cache
Delete cache
Deletes a cache
Attributes:
Name | Type | Description |
---|---|---|
cacheName | String | Name of the cache to be deleted. |
- JavaScript
- Python
- Java
- Go
- C#
- Rust
- Ruby
- CLI
const authToken="eyJhbGc.MyTestToken";
const defaultTTL = 15;
const momento = new SimpleCacheClient(authToken, defaultTtl);
momento.deleteCache('test-cache');
import momento.simple_cache_client as scc
_MOMENTO_AUTH_TOKEN = "eyJhbGc.MyTestToken"
_ITEM_DEFAULT_TTL_SECONDS = 15
with scc.SimpleCacheClient(_MOMENTO_AUTH_TOKEN, _ITEM_DEFAULT_TTL_SECONDS) as cache_client:
cache_client.delete_cache('test-cache')
String MOMENTO_AUTH_TOKEN = "eyJhbGc.MyTestToken";
int DEFAULT_ITEM_TTL_SECONDS = 15;
SimpleCacheClient simpleCacheClient = SimpleCacheClient
.builder(MOMENTO_AUTH_TOKEN, DEFAULT_ITEM_TTL_SECONDS)
.build()
simpleCacheClient.deleteCache("test-cache");
const (
authToken = "eyJhbGc.MyTestToken"
itemDefaultTtlSeconds = 15
)
client, err := momento.NewSimpleCacheClient(authToken, itemDefaultTtlSeconds)
if err != nil {
panic(err)
}
err = client.DeleteCache(&momento.CreateCacheRequest{
CacheName: "test-cache",
})
TimeSpan DEFAULT_TTL = TimeSpan.FromSeconds(15);
ICredentialProvider authProvider = new StringMomentoTokenProvider("eyJhbGc.MyTestToken");
using SimpleCacheClient client = new SimpleCacheClient(Configurations.Laptop.Latest(), authProvider, DEFAULT_TTL);
await client.DeleteCacheAsync("test-cache");
let auth_token = "eyJhbGc.MyTestToken";
let item_default_ttl_seconds = 15;
let mut cache_client = SimpleCacheClientBuilder::new(
auth_token,
NonZeroU64::new(item_default_ttl_seconds).unwrap(),
)
.unwrap()
.build();
let cache_name = String::from("test-cache");
match cache_client.delete_cache(&cache_name).await {
Ok(_) => {}
Err(err) => {
eprintln!("{}", err);
}
}
require 'momento'
MOMENTO_AUTH_TOKEN = "eyJhbGc.MyTestToken"
DEFAULT_TTL_SECONDS = 15
client = Momento::SimpleCacheClient.new(
auth_token: MOMENTO_AUTH_TOKEN, default_ttl: DEFAULT_TTL_SECONDS
)
response = client.delete_cache('test-cache')
raise repsonse.error if response.error?
momento cache delete-cache --name test-cache
List caches
Lists all caches for the provided auth token.
Name | Type | Description |
---|---|---|
nextToken | String | Token for pagination of caches. |
- JavaScript
- Python
- Java
- Go
- C#
- Rust
- Ruby
- CLI
require 'momento'
MOMENTO_AUTH_TOKEN = "eyJhbGc.MyTestToken"
DEFAULT_TTL_SECONDS = 15
client = Momento::SimpleCacheClient.new(
auth_token: MOMENTO_AUTH_TOKEN, default_ttl: DEFAULT_TTL_SECONDS
)
puts client.caches.to_a.join(", ")
momento cache list
Flush cache
Flushes all data from a cache
Attributes:
Name | Type | Description |
---|---|---|
cacheName | String | Name of the cache to be flushed. |
While you could use Delete Cache, then Create Cache to mimic this, the FlushCache API keeps the settings and only deletes the data in the cache.
Data APIs
These API methods are used to directly interact with data in a cache.
Set
Sets the value in cache with a given Time To Live (TTL) seconds. If a value for this key is already present it will be replaced by the new value.
Name | Type | Description |
---|---|---|
cacheName | String | Name of the cache. |
key | []Byte | The key under which the value is to be added. |
value | []Byte | The value to be stored. |
ttlSeconds | int | Time to Live for the item in Cache. |
- JavaScript
- Python
- Java
- Go
- C#
- Rust
- Ruby
- CLI
const authToken="eyJhbGc.MyTestToken";
const defaultTTL = 15;
const momento = new SimpleCacheClient(authToken, defaultTtl);
momento.set('test-cache', 'test-key', 'test-value');
import momento.simple_cache_client as scc
_MOMENTO_AUTH_TOKEN = "eyJhbGc.MyTestToken"
_ITEM_DEFAULT_TTL_SECONDS = 15
with scc.SimpleCacheClient(_MOMENTO_AUTH_TOKEN, _ITEM_DEFAULT_TTL_SECONDS) as cache_client:
cache_client.set('test-cache', 'test-key', 'test-value')
String MOMENTO_AUTH_TOKEN = "eyJhbGc.MyTestToken";
int DEFAULT_ITEM_TTL_SECONDS = 15;
SimpleCacheClient simpleCacheClient = SimpleCacheClient
.builder(MOMENTO_AUTH_TOKEN, DEFAULT_ITEM_TTL_SECONDS)
.build()
simpleCacheClient.set('test-cache', 'test-key', 'test-value');
const (
authToken = "eyJhbGc.MyTestToken"
itemDefaultTtlSeconds = 15
)
client, err := momento.NewSimpleCacheClient(authToken, itemDefaultTtlSeconds)
if err != nil {
panic(err)
}
_, err = client.Set(&CacheSetRequest{
CacheName: "test-cache",
Key: "test-key",
Value: "test-value",
})
TimeSpan DEFAULT_TTL = TimeSpan.FromSeconds(15);
ICredentialProvider authProvider = new StringMomentoTokenProvider("eyJhbGc.MyTestToken");
using SimpleCacheClient client = new SimpleCacheClient(Configurations.Laptop.Latest(), authProvider, DEFAULT_TTL);
await client.SetAsync("test-cache", "test-key", "test-value");
let auth_token = "eyJhbGc.MyTestToken";
let item_default_ttl_seconds = 15;
let mut cache_client = SimpleCacheClientBuilder::new(
auth_token,
NonZeroU64::new(item_default_ttl_seconds).unwrap(),
)
.unwrap()
.build();
let cache_name = String::from("cache");
let key = String::from("my_key");
let value = String::from("my_value");
cache_client
.set(&cache_name, key.clone(), value.clone(), None)
.await
.unwrap();
require 'momento'
MOMENTO_AUTH_TOKEN = "eyJhbGc.MyTestToken"
DEFAULT_TTL_SECONDS = 15
client = Momento::SimpleCacheClient.new(
auth_token: MOMENTO_AUTH_TOKEN, default_ttl: DEFAULT_TTL_SECONDS
)
response = client.set('test-cache', 'test-key', 'test-value')
raise response.error if response.error?
momento cache set --key test-key --value test-value
Get
Get the cache value stored for the given key.
Name | Type | Description |
---|---|---|
cacheName | String | Name of the cache. |
key | []Byte | The key under which the value is to be added. |
- JavaScript
- Python
- Java
- Go
- C#
- Rust
- Ruby
- CLI
const authToken="eyJhbGc.MyTestToken";
const defaultTTL = 15;
const momento = new SimpleCacheClient(authToken, defaultTtl);
momento.get('test-cache', 'test-key');
import momento.simple_cache_client as scc
_MOMENTO_AUTH_TOKEN = "eyJhbGc.MyTestToken"
_ITEM_DEFAULT_TTL_SECONDS = 15
with scc.SimpleCacheClient(_MOMENTO_AUTH_TOKEN, _ITEM_DEFAULT_TTL_SECONDS) as cache_client:
cache_client.get('test-cache', 'test-key')
String MOMENTO_AUTH_TOKEN = "eyJhbGc.MyTestToken";
int DEFAULT_ITEM_TTL_SECONDS = 15;
SimpleCacheClient simpleCacheClient = SimpleCacheClient
.builder(MOMENTO_AUTH_TOKEN, DEFAULT_ITEM_TTL_SECONDS)
.build()
simpleCacheClient.get('test-cache', 'test-key');
const (
authToken = "eyJhbGc.MyTestToken"
itemDefaultTtlSeconds = 15
)
client, err := momento.NewSimpleCacheClient(authToken, itemDefaultTtlSeconds)
if err != nil {
panic(err)
}
_, err = client.Get(&CacheSetRequest{
CacheName: "test-cache",
Key: "test-key",
})
TimeSpan DEFAULT_TTL = TimeSpan.FromSeconds(15);
ICredentialProvider authProvider = new StringMomentoTokenProvider("eyJhbGc.MyTestToken");
using SimpleCacheClient client = new SimpleCacheClient(Configurations.Laptop.Latest(), authProvider, DEFAULT_TTL);
await client.GetAsync("test-cache", "test-key");
let auth_token = "eyJhbGc.MyTestToken";
let item_default_ttl_seconds = 15;
let mut cache_client = SimpleCacheClientBuilder::new(
auth_token,
NonZeroU64::new(item_default_ttl_seconds).unwrap(),
)
.unwrap()
.build();
let cache_name = String::from("cache");
let key = String::from("my_key");
let value = String::from("my_value");
cache_client
.get(&cache_name, key.clone())
.await
.unwrap();
require 'momento'
MOMENTO_AUTH_TOKEN = "eyJhbGc.MyTestToken"
DEFAULT_TTL_SECONDS = 15
client = Momento::SimpleCacheClient.new(
auth_token: MOMENTO_AUTH_TOKEN, default_ttl: DEFAULT_TTL_SECONDS
)
response = client.get('test-cache', 'test-key')
if response.hit?
puts response.value_string
elsif response.miss?
puts "The item was not in the cache."
elsif response.error?
raise response.error
end
momento cache get --key test-key --value test-value
Delete
Delete the cache value stored for the given key.
Name | Type | Description |
---|---|---|
cacheName | String | Name of the cache. |
key | []Byte | The key under which the value is to be deleted. |
- JavaScript
- Python
- Java
- Go
- C#
- Rust
- Ruby
- CLI
const authToken="eyJhbGc.MyTestToken";
const defaultTTL = 15;
const momento = new SimpleCacheClient(authToken, defaultTtl);
momento.delete('test-cache', 'test-key');
import momento.simple_cache_client as scc
_MOMENTO_AUTH_TOKEN = "eyJhbGc.MyTestToken"
_ITEM_DEFAULT_TTL_SECONDS = 15
with scc.SimpleCacheClient(_MOMENTO_AUTH_TOKEN, _ITEM_DEFAULT_TTL_SECONDS) as cache_client:
cache_client.delete('test-cache', 'test-key')
String MOMENTO_AUTH_TOKEN = "eyJhbGc.MyTestToken";
int DEFAULT_ITEM_TTL_SECONDS = 15;
SimpleCacheClient simpleCacheClient = SimpleCacheClient
.builder(MOMENTO_AUTH_TOKEN, DEFAULT_ITEM_TTL_SECONDS)
.build()
simpleCacheClient.delete('test-cache', 'test-key');
const (
authToken = "eyJhbGc.MyTestToken"
itemDefaultTtlSeconds = 15
)
client, err := momento.NewSimpleCacheClient(authToken, itemDefaultTtlSeconds)
if err != nil {
panic(err)
}
_, err = client.Delete(&CacheSetRequest{
CacheName: "test-cache",
Key: "test-key",
})
TimeSpan DEFAULT_TTL = TimeSpan.FromSeconds(15);
ICredentialProvider authProvider = new StringMomentoTokenProvider("eyJhbGc.MyTestToken");
using SimpleCacheClient client = new SimpleCacheClient(Configurations.Laptop.Latest(), authProvider, DEFAULT_TTL);
await client.DeleteAsync("test-cache", "test-key");
let auth_token = "eyJhbGc.MyTestToken";
let item_default_ttl_seconds = 15;
let mut cache_client = SimpleCacheClientBuilder::new(
auth_token,
NonZeroU64::new(item_default_ttl_seconds).unwrap(),
)
.unwrap()
.build();
let cache_name = String::from("cache");
let key = String::from("my_key");
let value = String::from("my_value");
cache_client
.delete(&cache_name, key.clone())
.await
.unwrap();
require 'momento'
MOMENTO_AUTH_TOKEN = "eyJhbGc.MyTestToken"
DEFAULT_TTL_SECONDS = 15
client = Momento::SimpleCacheClient.new(
auth_token: MOMENTO_AUTH_TOKEN, default_ttl: DEFAULT_TTL_SECONDS
)
response = client.delete('test-cache', 'test-key')
raise response.error if response.error?
momento cache delete --key test-key --value test-value
Increment
Adds to the value of a field, if and only if the existing value is a UTF-8 string representing a base 10 integer. If the field does not exist, this method sets the field's value to the amount to increment by.
Name | Type | Description |
---|---|---|
cacheName | String | Name of the cache. |
field | String | The key under which the value is to be deleted. |
amount | Integer | The quantity to add to the value. May be positive, negative, or zero. Defaults to 1. |
The resulting incremented value must be between -9223372036854775808 and 9223372036854775807, ie. a signed 64-bit integer. If not, there will be an error response.
Ping
Sends a ping to the server. This API can be used for checking connectivity to confirm that the client can connect to the server successfully.
Method response object
* Success
* Error
See [response objects](/ja/develop/api-reference/response-objects) for specific information.
KeyExists
Checks if a provided key exists in the cache.
Name | Type | Description |
---|---|---|
cacheName | String | Name of the cache. |
key | String | byte | Key which is to be checked for its existence in the cache. |
Method response object
* Success
- `exists()`: bool
* Error
See [response objects](/ja/develop/api-reference/response-objects) for specific information.
KeysExist
Checks if provided keys exist in the cache.
Name | Type | Description |
---|---|---|
cacheName | String | Name of the cache. |
keys | String[] | byte[] | Keys which are to be checked for their existence in the cache. |
Method response object
* Success
- `exists()`: bool[]
* Error
See [response objects](/ja/develop/api-reference/response-objects) for specific information.
SetIfNotExists
Associates the provided value to a cache item with a given key.
Name | Type | Description |
---|---|---|
cacheName | String | Name of the cache. |
key | String / | bytes |
value | String / | bytes |
ttlSeconds | Duration | Time to Live for the item in Cache. |
Method response object
* Stored
* NotStored
* Error
See [response objects](/ja/develop/api-reference/response-objects) for specific information.
Time to Live APIs
These APIs apply across all cache types.
UpdateTtl
Overwrites the TTL of a cache item with the provided value in seconds.
Name | Type | Description |
---|---|---|
cacheName | String | Name of the cache. |
key | String | bytes | The key under which the value's TTL is to be updated. |
ttl | Duration | Time to live that you want to update in cache in seconds. |
IncreaseTtl
Increase the TTL seconds for a key to the provided value only if it would increase the TTL.
Examples
- If the TTL is 5 seconds and is increased to 6 seconds, the new TTL will be 6 seconds.
- If the TTL is 5 seconds and is increased to 3 seconds, the TTL will not be increased.
Name | Type | Description |
---|---|---|
cacheName | String | Name of the cache. |
key | String | bytes | The key under which the value's TTL is to be increased. |
ttl | Duration | Time to live that you want to increase to. |
DecreaseTtl
Decrease the TTL seconds for a key to the provided value only if it would decrease the TTL.
Examples
- If the TTL is 5 seconds and is decreased to 3 seconds, the new TTL will be 3 seconds.
- If the TTL is 5 seconds and is decreased to 6 seconds, the TTL will not be decreased.
Name | Type | Description |
---|---|---|
cacheName | String | Name of the cache. |
key | String | bytes | The key under which the value's TTL is to be decreased. |
ttl | Duration | Time to live that you want to decrease to. |
Collection data types (CDTs)
Collections may contain different types of structures depending on your use case. Supported data types are:
- Dictionaries are used to store unordered field:value pairs.
- Lists are a collection of ordered elements, sorted in the sequence each element was inserted.
- Sets are an unordered collection of unique elements in string format.
For more in-depth information on usage, see collection data types.
Current status of API support in SDKs
Feature | .NET | Java | Node.js | Python | PHP | Ruby | Go | Rust |
---|---|---|---|---|---|---|---|---|
Control APIs | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
Get/Set | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
Increment | ❌ | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ |
Dictionary CDTs | ✅ | ❌ | ✅ | ❌ | ✅ | ❌ | ❌ | ❌ |
Set CDTs | ✅ | ❌ | ✅ | ❌ | ✅ | ❌ | ❌ | ❌ |
List CDTs | ✅ | ❌ | ✅ | ❌ | ✅ | ❌ | ❌ | ❌ |
Flush Cache | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ |