@compass/cache

This package provides functionalities to interact with the Compass cache system. You can retrieve, set and manage cache data.

Usage

import cache from "@compass/cache";

API Reference

cache.get(key: string)

Retrieves a value from the cache. Returns null if the key does not exist.

  • key (required, string): The key to retrieve.

Returns a Promise that resolves with the value associated with the key in the cache.

cache.set(key: string, value: any, ttl: number = 60 * 60)

Sets a value in the cache. If a value already exists for the key, it will be overwritten.

  • key (required, string): The key to set.

  • value (required, any): The value to set.

  • ttl (optional, number): The time to live in seconds. Defaults to 60 minutes (60 * 60).

Returns a Promise that resolves once the key-value pair is successfully stored in the cache.

Examples

To retrieve a value from the cache:

import cache from "@compass/cache";

export default async function() {
    const key = 'myKey';
    const value = await cache.get(key);

    return {
      value,
    }
}

To set a value in the cache:

import cache from "@compass/cache";

export default async function() {
    const key = 'myKey';
    const value = 'Hello, World!';
    const ttl = 60 * 60; // 1 hour

    await cache.set(key, value, ttl);
    
    return {
      success: true,
    }
}

Last updated