The Cache API is now available as part of Forge’s Early Access Program (EAP). To start using this EAP, sign up using this form.
Forge’s EAP offers experimental features to selected users for testing and feedback purposes. These features are not supported or recommended for use in production environments. They are also subject to change without notice.
For more information, see Forge EAP, Preview, and GA.
You can use the Cache API to store temporary, ephemeral data for fast access. Data stored through this API automatically expires based on the time to live (TTL) setting. For persistent hosted storage, use the Key-Value Store or Custom Entity Store instead.
To start using the Cache API, install the package in your project by running:
1 2npm install @forge/cache
Next, import the package into your app:
1 2import cache from '@forge/cache'; // ... const App = () => { // ... const cacheClient = cache.connect();
Each installation of your app is subject to the limits of Cache API.
Resource | Limit | Description |
---|---|---|
Key length | 500 characters | The maximum length of a key. |
Key format | Regex: /^(?!\s+$)[a-zA-Z0-9:._\s-#]+$/ | Keys must match the regex. |
Value size | 240 KiB | The maximum size of a single persisted value. |
Time to live (TTL) | Between 30s and 3600s | The duration (in seconds) that the key will exist in the cache before it's automatically deleted. The default value is 600s. |
@forge/api
is also supported. Otherwise, you’ll need to invoke your cache function through a resolver (like all back end functions).rpop
, lpush
and scan
methods may appear in @forge/cache
but they are not available and will not work during EAP.We’re currently evaluating a suitable minimum and maximum TTL. These values may change partly based on the data collected during the EAP. As such, we strongly suggest that you set appropriate TTL values based on your app’s needs.
If your app requires a higher limit, request a TTL increase through this form. We will review each request and adjust the limit based on demand, available resources, and use case validity.
During EAP, forge tunnel
will not correctly work with the cache.
Meanwhile, if you need to use the tunnel, you can work around this by emulating the Cache API locally. Refer to this snippet for an example. A secondary function in the getClient
will automatically detect when used in the tunnel. This function only works with the New native Node.js runtime.
Sets a key to a specific string value in the cache. This method replaces any existing value the key currently holds and discards any previous TTL associated with the key.
Use the set
operation to create a TTL associated with the key.
Method signature
1 2cacheClient.set(key, value, { ttlSeconds }): Promise<void>;
Example
1 2await cacheClient.set('example-key', 'example-value');
Set a key to hold a string value only if the key does not already exist. It will return null
if the key already exists and OK
if the key does not exist and has now been overwritten.
Method signature
1 2cacheClient.setIfNotExists(key, value, { ttlSeconds }): Promise<null | 'OK'>;
Example
1 2const response = await cacheClient.setIfNotExists('example-key', 'example-value');
Get the value of a key. If the key does not exist, null
is returned. If the key does exist, it will return the string value.
Method signature
1 2cacheClient.get(key): Promise<value>;
Example
1 2const value = await cacheClient.get('example-key');
Increments the integer value stored at the specified string key by one. If the key does not exist, it is set to 0
before performing the operation and uses the default value (600s) for TTL. If the value at the key is not an integer, the system will return an error. After the increment operation, it returns the value of the key after the increment.
Method signature
1 2cacheClient.incrementAndGet(key): Promise<value>;
Example
1 2const value = await cacheClient.incrementAndGet('example-key');
Removes the specified key and its corresponding value. If the operation is successful, it will return 1
and if it can't find the key, it will return 0
.
Method signature
1 2cacheClient.delete(key): Promise<numberOfKeyDeleted>;
Example
1 2const numberOfKeysDeleted = await cacheClient.delete('example-key');
Decrements the number stored at key by one. If the key does not exist, it is set to 0
before performing the operation and will use the default value (600s) for TTL. If the value at the key is not an integer, an error will be returned. After the decrement operation, it returns the value of key after the decrement.
Method signature
1 2cacheClient.decrementAndGet(key): Promise<value>;
Example
1 2const value = await cacheClient.decrementAndGet('example-key');
Gets and returns the old value of the specified key, then sets the new value. If the key does not exist, null
is returned.
Method signature
1 2cacheClient.getAndSet(key, value, { ttlSeconds }): Promise<null | string>;
Example
1 2const response = await cacheClient.getAndSet('example-key', 'example-value');
Rate this page: