Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-ts-cache

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-ts-cache

Simple and extensible caching module supporting decorators

  • 4.4.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
8.9K
increased by2.63%
Maintainers
1
Weekly downloads
 
Created
Source

CI CI npm The MIT License

NPM

node-ts-cache

Simple and extensible caching module supporting decorators.

Install

npm install node-ts-cache

Note: The underlying storage layer must be installed separately.

Storages

StorageInstall
memorynpm install node-ts-cache-storage-memory
node-fsnpm install node-ts-cache-storage-node-fs
ioredisnpm install node-ts-cache-storage-ioredis

Usage

With decorator

Caches function response using the given options. Works with the above listed storages. By default, uses all arguments to build an unique key.

@Cache(container, options)

  • options:
    • ttl: (Default: 60) Number of seconds to expire the cachte item
    • isLazy: (Default: true) If true, expired cache entries will be deleted on touch. If false, entries will be deleted after the given ttl.
    • isCachedForever: (Default: false) If true, cache entry has no expiration.
    • calculateKey(data => string): (Default: JSON.stringify combination of className, methodName and call args)
      • data:
        • className: The class name for the method being decorated
        • methodName: The method name being decorated
        • args: The arguments passed to the method when called

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

Directly

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

const myCache = new CacheContainer(new MemoryStorage())

class MyService {
    public async getUsers(): Promise<string[]> {
        const 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 lerna. To start development and run tests for all the packages, run:

git clone git@github.com:havsar/node-ts-cache.git
cd node-ts-cache
npm i
npm run bootstrap
npm run test

Keywords

FAQs

Package last updated on 11 Oct 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc