Clean value(s) from cache
import { Corva } from '@corva/node-sdk';
export const handler = new Corva().scheduled(async (event, { cache }) => {
await cache.store({ key1: 'val1', key2: 'val2', key3: 'val3' });
// cache: { key1: 'val1', key2: 'val2', key3: 'val3' }
await cache.delete('key1');
// cache: { key2: 'val2', key3: 'val3' }
return { status: 'OK' };
});
Rest
...keys: string[]Remove a few elements from cache
import { Corva } from '@corva/node-sdk';
export const handler = new Corva().scheduled(async (event, { cache }) => {
await cache.store({ key1: 'value1', key2: 'value2', key3: 'value3' });
// cache: { key1: 'value1', key2: 'value2', key3: 'value3' }
await cache.deleteMany(['key1', 'key2']);
// cache: { key3: 'value3' }
return { status: 'OK' };
});
Load cache value by provided key
import { Corva } from '@corva/node-sdk';
export const handler = new Corva().scheduled(async (event, { cache, logger }) => {
await cache.store({ key: 'val' });
const res = await cache.load('key');
logger.debug(res); // val
return { status: 'OK' };
});
Load multiple cache values by provided keys
import { Corva } from '@corva/node-sdk';
export const handler = new Corva().scheduled(async (event, { cache, logger }) => {
await cache.store({ key1: 'val1', key2: 'val2', key3: 'val3' });
const res = await cache.loadAll();
logger.debug(res); // { key1: 'val1', key2: 'val2' }
return { status: 'OK' };
});
Rest
...keys: string[]Save multiple values
⚠️⚠️⚠️ Cache can store only string data. Cast your data to string before saving.
ℹ️ By default, Cache sets an expiry to 60 days.
import { Corva } from '@corva/node-sdk';
export const handler = new Corva().scheduled(async (event, { cache, logger }) => {
await cache.store({ key: 'val' });
const res = await cache.load('key');
logger.debug(res); // val
return { status: 'OK' };
});
Optional
expiry: numberOptional
expiry: number
Apps might need to share some data between runs. The SDK provides a State class that allows you to store, load, and do other operations with data.
State instance is inserted automatically to stream and scheduled apps' StatefulContext
⚠️⚠️⚠️ Task apps don't get a State inside StatelessContext as they aren't meant to store data between invokes.
ℹ️ State uses a dict-like database, so the data is stored as
key:value
pairs. ℹ️key
should be ofstring
type, andvalue
can have any of the following types:string
,Buffer
,number
,any[]
.The SDK by itself creates the unique key under the hood, so no need to add well id/company name/etc. to the keys.
Use case
A typical example of Cache usage is:
ℹ️ To get the most out of Cache