Skip to content

Latest commit

 

History

History
143 lines (93 loc) · 5.02 KB

README.md

File metadata and controls

143 lines (93 loc) · 5.02 KB

@ioki/node-ts-cache

CI The MIT License Coverage Status

Simple and extensible caching module supporting decorators.

Install

yarn add @ioki/node-ts-cache

Note: The underlying storage layer must be installed separately.

Storage

Storage Install
memory yarn add @ioki/node-ts-cache-storage-memory
node-fs yarn add @ioki/node-ts-cache-storage-node-fs
ioredis yarn add @ioki/node-ts-cache-storage-ioredis
postgres yarn add @ioki/node-ts-cache-storage-pg
elasticsearch yarn add @ioki/node-ts-cache-storage-elasticsearch

Usage

Wrap your function calls withCacheFactory

Function wrapper factory for arbitrary functions. The cache key is caculated based on the parameters passed to the function.

import { withCacheFactory, CacheContainer } from '@ioki/node-ts-cache'
import { MemoryStorage } from '@ioki/node-ts-cache-storage-memory'

const doThingsCache = new CacheContainer(new MemoryStorage())

const someFn = (input: { a: string, b: number })

const wrappedFn = withCacheFactory(doThingsCache)(someFn);

const result = someFn({ a: "lala", b: 123 })

@Cache decorator

Caches function response using the given options. By default, uses all arguments to build an unique key.

Note: @Cache will consider the return type of the function. If the return type is a thenable, it will stay that way, otherwise not.

import { Cache, CacheContainer } from '@ioki/node-ts-cache'
import { MemoryStorage } from '@ioki/node-ts-cache-storage-memory'

const userCache = new CacheContainer(new MemoryStorage())

class MyService {
    @Cache(userCache, {ttl: 60})
    public async getUsers(): Promise<string[]> {
        return ["Max", "User"]
    }
}

Using getItem and setItem directly

import { CacheContainer } from '@ioki/node-ts-cache'
import { MemoryStorage } from '@ioki/node-ts-cache-storage-memory'

const myCache = new CacheContainer(new MemoryStorage())

class MyService {
    public async getUsers(): Promise<string[]> {
        const { content: cachedUsers } = await myCache.getItem<string[]>("users")

        if (cachedUsers) {
            return cachedUsers
        }

        const newUsers = ["Max", "User"]

        await myCache.setItem("users", newUsers, {ttl: 60})

        return newUsers
    }
}

Logging

This project uses debug to log useful caching information. Set environment variable DEBUG=node-ts-cache to enable logging.

Mocking

Just use the memory storage adapter in your tests.

LICENSE

Distributed under the MIT License. See LICENSE.md for more information.

Development & Testing

This project follows the monorepo architecture using yarn workspaces.

To start development and run tests for all the packages, run:

cd node-ts-cache
yarn
yarn build
yarn test

Release

We're using changeset to automate the release process. The only thing to be done is to commit a changeset.

Credits

As this is a fork of the original node-ts-cache, all credit goes to the upstream project by havsar.

Structural changes have been made by boredland in order to align more with our use-case.

Contributing (complexity, asc)

  1. join us @ioki and make this one of your projects
  2. create issues and pull requests, we're happy to enhance this

Contact

ioki logo

ioki Mobility - @ioki_mobility

Project Link: https://github.com/ioki-mobility/node-ts-cache