Skip to main content

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.

info

Momento collection types use a CollectionTTL to specify their TTL behavior. This is an optional argument for all "write" operations.

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.

NameTypeDescription
cacheNameStringName of the cache.
setNameStringName of the set item to be altered.
elementString | BytesElement to be added by this operation.
ttlCollectionTTL objectTTL 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.

const result = await cacheClient.setAddElement(cacheName, '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 '${cacheName}': ${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.

NameTypeDescription
cacheNameStringName of the cache.
setNameStringName of the set item to be altered.
elementsString[] | Bytes[]Elements to be added by this operation.
ttlCollectionTTL objectTTL 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.

const result = await cacheClient.setAddElements(cacheName, '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 '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}

SetFetch

Gets a set item from a cache.

NameTypeDescription
cacheNameStringName of the cache.
setNameStringThe 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.

await cacheClient.setAddElements(cacheName, 'test-set', ['test-element1', 'test-element2']);
const result = await cacheClient.setFetch(cacheName, '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 '${cacheName}'`);
} else if (result instanceof CacheSetFetch.Error) {
throw new Error(
`An error occurred while attempting to call cacheSetFetch on set 'test-set' in cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}

SetSample

Gets a random sampling of elements from a set item in a cache.

NameTypeDescription
cacheNameStringName of the cache.
setNameStringThe name of the set item to sample.
limitNumberThe maximum number of elements to be retrieved.
Method response object

The response object for SetSample 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.

await cacheClient.setAddElements(cacheName, 'test-set', ['test-element1', 'test-element2', 'test-element3']);
const result = await cacheClient.setSample(cacheName, 'test-set', 2);
if (result instanceof CacheSetSample.Hit) {
console.log('Sample of 2 elements fetched successfully- ');
result.valueSet().forEach((value, key) => {
console.log(`${key} : ${value}`);
});
} else if (result instanceof CacheSetSample.Miss) {
console.log(`Set 'test-set' was not found in cache '${cacheName}'`);
} else if (result instanceof CacheSetSample.Error) {
throw new Error(
`An error occurred while attempting to call cacheSetSample on set 'test-set' in cache '${cacheName}': ${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.

NameTypeDescription
cacheNameStringName of the cache.
setNameStringName of the set item to be altered.
elementString | BytesElement to be removed by this operation.
Method response object
  • Success
  • Error

See response objects for specific information.

await cacheClient.setAddElement(cacheName, 'test-set', 'test-element');
const result = await cacheClient.setRemoveElement(cacheName, '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 '${cacheName}': ${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.

NameTypeDescription
cacheNameStringName of the cache.
setNameStringName of the set item to be altered.
elementString[] | Bytes[]Elements to be removed by this operation.
Method response object
  • Success
  • Error

See response objects for specific information.

await cacheClient.setAddElements(cacheName, 'test-set', ['test-element1', 'test-element2']);
const result = await cacheClient.setRemoveElements(cacheName, '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 '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}

SetContainsElement

Checks if a provided element is in the given set.

NameTypeDescription
cacheNameStringName of the cache.
setNameStringName of the set item.
elementString | BytesName 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.

NameTypeDescription
cacheNameStringName of the cache.
setNameStringName of the set item.
elementsString[] | 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

NameTypeDescription
cacheNameStringName of the cache.
setNameStringName of the set item to be checked.
Method response object
  • Hit
    • length(): Number
  • Miss
  • Error

See response objects for specific information.