Set API reference for Momento Cache
A set is a collection of elements, but each element can appear only once and order is not guaranteed.
Example: if your set contains [1, 2, 3]
and you add 2, the set remains [1, 2, 3].
See Sets for more information on their usage.
Set methods
SetAddElement
Adds an element to a set. If the set item does not already exist, this method will create one.
Name | Type | Description |
---|---|---|
cacheName | String | Name of the cache. |
setName | String | Name of the set item to be altered. |
element | String | Bytes | Element to be added by this operation. |
ttl | CollectionTTL object | TTL for the set item in cache. This TTL takes precedence over the TTL used when initializing a cache connection client. |
Method response object
- Success
- Error
See response objects for specific information.
- JavaScript
const result = await cacheClient.setAddElement('test-cache', 'test-set', 'test-element');
if (result instanceof CacheSetAddElement.Success) {
console.log("Element added successfully to set 'test-set'");
} else if (result instanceof CacheSetAddElement.Error) {
throw new Error(
`An error occurred while attempting to call cacheSetAddElement on set 'test-set' in cache 'test-cache': ${result.errorCode()}: ${result.toString()}`
);
}
SetAddElements
Adds multiple elements to a set item. If the set item does not already exist, this method will create one.
Name | Type | Description |
---|---|---|
cacheName | String | Name of the cache. |
setName | String | Name of the set item to be altered. |
elements | String[] | Bytes[] | Elements to be added by this operation. |
ttl | CollectionTTL object | TTL for the set item in cache. This TTL takes precedence over the TTL used when initializing a cache connection client. |
Method response object
- Success
- Error
See response objects for specific information.
- JavaScript
const result = await cacheClient.setAddElements('test-cache', 'test-set', ['test-element1', 'test-element2']);
if (result instanceof CacheSetAddElements.Success) {
console.log("Elements added successfully to set 'test-set'");
} else if (result instanceof CacheSetAddElements.Error) {
throw new Error(
`An error occurred while attempting to call cacheSetAddElements on set 'test-set' in cache 'test-cache': ${result.errorCode()}: ${result.toString()}`
);
}
SetFetch
Gets a set item from a cache.
Name | Type | Description |
---|---|---|
cacheName | String | Name of the cache. |
setName | String | The name of the set item to be retrieved. |
Method response object
The response object for SetFetch returns three possible options, a cache hit, miss, or an error.
- Hit
- valueSetBytes(): Bytes[]
- valueSetString(): String[]
- toString(): String
- Miss
- Error
See response objects for specific information.
- JavaScript
await cacheClient.setAddElements('test-cache', 'test-set', ['test-element1', 'test-element2']);
const result = await cacheClient.setFetch('test-cache', 'test-set');
if (result instanceof CacheSetFetch.Hit) {
console.log('Set fetched successfully- ');
result.valueSet().forEach((value, key) => {
console.log(`${key} : ${value}`);
});
} else if (result instanceof CacheSetFetch.Miss) {
console.log("Set 'test-set' was not found in cache 'test-cache'");
} else if (result instanceof CacheSetFetch.Error) {
throw new Error(
`An error occurred while attempting to call cacheSetFetch on set 'test-set' in cache 'test-cache': ${result.errorCode()}: ${result.toString()}`
);
}
SetRemoveElement
Removes a single element from an existing set item. If the set is emptied as a result, the item is deleted.
Name | Type | Description |
---|---|---|
cacheName | String | Name of the cache. |
setName | String | Name of the set item to be altered. |
element | String | Bytes | Element to be removed by this operation. |
Method response object
- Success
- Error
See response objects for specific information.
- JavaScript
await cacheClient.setAddElement('test-cache', 'test-set', 'test-element');
const result = await cacheClient.setRemoveElement('test-cache', 'test-set', 'test-element');
if (result instanceof CacheSetRemoveElement.Success) {
console.log("Element 'test-element' removed successfully from set 'test-set'");
} else if (result instanceof CacheSetRemoveElement.Error) {
throw new Error(
`An error occurred while attempting to call cacheSetRemoveElement on set 'test-set' in cache 'test-cache': ${result.errorCode()}: ${result.toString()}`
);
}
SetRemoveElements
Removes multiple elements from an existing set item. If the set is emptied as a result, the item is deleted.
Name | Type | Description |
---|---|---|
cacheName | String | Name of the cache. |
setName | String | Name of the set item to be altered. |
element | String[] | Bytes[] | Elements to be removed by this operation. |
Method response object
- Success
- Error
See response objects for specific information.
- JavaScript
await cacheClient.setAddElements('test-cache', 'test-set', ['test-element1', 'test-element2']);
const result = await cacheClient.setRemoveElements('test-cache', 'test-set', ['test-element1', 'test-element2']);
if (result instanceof CacheSetRemoveElements.Success) {
console.log("Elements 'test-element1' and 'test-element2' removed successfully from set 'test-set'");
} else if (result instanceof CacheSetRemoveElements.Error) {
throw new Error(
`An error occurred while attempting to call cacheSetRemoveElements on set 'test-set' in cache 'test-cache': ${result.errorCode()}: ${result.toString()}`
);
}
SetContainsElement
Checks if a provided element is in the given set.
Name | Type | Description |
---|---|---|
cacheName | String | Name of the cache. |
setName | String | Name of the set item. |
element | String | Bytes | Name of the element to check existence of. |
Method response object
The response object for SetContainsElement returns three possible options, a cache hit, miss, or an error.
- Hit
- containsElement(): bool
- Miss
- Error
See response objects for specific information.
SetContainsElements
Checks if provided elements are in the given set.
Name | Type | Description |
---|---|---|
cacheName | String | Name of the cache. |
setName | String | Name of the set item. |
elements | String[] | Bytes[] | Array of element names to check existence of. |
Method response object
The response object for SetContainsElements returns three possible options, a cache hit, miss, or an error.
- Hit
- containsElements(): bool[]
- Miss
- Error
See response objects for specific information.
SetLength
Get the length of an existing set item
Name | Type | Description |
---|---|---|
cacheName | String | Name of the cache. |
setName | String | Name of the set item to be checked. |
Method response object
- Hit
length()
: Number
- Miss
- Error
See response objects for specific information.