Skip to content

Getting Started

Installation

bash
yarn add @apihive/adapter-simple-cache
bash
npm install @apihive/adapter-simple-cache
bash
jsr add @apihive/adapter-simple-cache

Enabling the adapter

ts
import { HTTPRequestFactory } from '@apihive/core';
import SimpleRequestCacheAdapter from '@apihive/adapter-simple-cache';


const requestFactory = new HTTPRequestFactory()
  .withAdapter(new SimpleRequestCacheAdapter());

// Cache the response for 60 seconds
const response = await requestFactory
  .createGETRequest('https://jsonplaceholder.typicode.com/users/1')
  .withMeta({ cache: 60 })
  .execute();

const cachedResponse = await requestFactory
  .createGETRequest('https://jsonplaceholder.typicode.com/users/1')
  .execute();

API-level caching configuration

When using an API, the adapter can be configured at the API level.

The adapter can be then configured with a shouldCache callback that can override the API-level configuration.

The shouldCache callback can return a boolean or an object with read and write properties. If it returns a boolean, it will be used as the value for both read and write.

It can be used, for instance, to invalidate the cache for specific endpoints, when the app receives a notification from the server that the data has changed.

Here's a crude example of how to implement it:

ts
import { HTTPRequestFactory, type APIConfig } from '@apihive/core';
import SimpleRequestCacheAdapter, { type SimpleRequestCacheAdapterOptions } from '@apihive/adapter-simple-cache';

const api: APIConfig = {
    name : 'default',
    meta : {
        cache : 3600
    },
    endpoints : {
        getUserPreferences : {
            target: '/user/preferences'
        }
    }
}

const endpointsToClear = new Set<string>();

const config : SimpleRequestCacheAdapterOptions = {
    cacheName : 'my-app-request-cache',
    shouldCache : (config : RequestConfig) => {
        if(endpointsToClear.has(config.meta?.api?.endpointName)) {
            endpointsToClear.delete(config.meta?.api?.endpointName);
            return { read : false, write : true };
        }
        return true
    }
}

const requestFactory = new HTTPRequestFactory()
    .withAdapter(new SimpleRequestCacheAdapter(config))

// When the app receives a notification from the server that the users db has changed
endpointsToClear.add('getUserPreferences');

Including body in the hash computation

By default, the adapter does not include the request body in the hash computation because, unless the body is used to query the server, it is not relevant to the cache key and it would add unnecessary computational time.

If you need to include the body in the hash computation, you can pass the hashBody option to the meta.cache property in its extended form.

ts
const response = await requestFactory
  .createPOSTRequest('https://mydomain.com/api/search-user')
  .withBody({ type : 'admin' })
  .withMeta({ cache: { ttlSeconds : 60, hashBody : true } })
  .execute();

The same configuration style can be used at the API level.