This package has been deprecated

Author message:

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

@boredland/node-ts-cache
TypeScript icon, indicating that this package has built-in type declarations

6.0.2 • Public • Published

@boredland/node-ts-cache

CI The MIT License Coverage Status

Simple and extensible caching module supporting decorators.

Install

yarn add @boredland/node-ts-cache

Note: The underlying storage layer must be installed separately.

Storage

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

Usage

withCacheFactory

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

import { withCacheFactory, CacheContainer } from '@boredland/node-ts-cache'
import { MemoryStorage } from '@boredland/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 })

With 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 '@boredland/node-ts-cache'
import { MemoryStorage } from '@boredland/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"]
    }
}

Direct usage

import { CacheContainer } from '@boredland/node-ts-cache'
import { MemoryStorage } from '@boredland/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.

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

Package Sidebar

Install

npm i @boredland/node-ts-cache

Weekly Downloads

0

Version

6.0.2

License

MIT

Unpacked Size

15.4 kB

Total Files

12

Last publish

Collaborators

  • jonasstr