npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cachetools-js

v1.0.1

Published

Best, faster, ligther, all-in-one, fully-typed cache library for JS, based on cachetools from python, with events, pooling and decorators.

Downloads

4

Readme

CacheTools-Js

Best, faster, ligther, all-in-one, fully-typed cache library for JS, based on cachetools from python, with events, pooling and decorators.

All of most used cache types are included here, if what you are looking for does not exist here, tell us.

Installation

With NPM

  npm install cachetools-js

With Yarn

  yarn add cachetools-js

How to Use

How to do operations in caches

Create a cache instance

//example cache
import { TTLCache } from 'cachetools-js'

const cache = new TTLCache({ttl: 1000})

Store a key in cache

cache['foo'] = 'bar' //recommended method
cache.set('foo', 'bar', {ttl: 500}) //second method, use this method if you wanna pass custom options

Get a key from cache

let key = cache['foo'] //recommended method
let key = cache.get('foo') //second method

Del a key from cache

cache.del('foo')

Take a key from cache - Get and Delete

let key = cache.take('foo')
cache['foo'] //will be undefined

Delete all keys

cache.delAll()

Get all keys

let keys = cache.keys()

Get all values

let values = cache.values()

Get cache length

let length = cache.length()

Events

Get event

cache.on('get', key => console.log(key))

Set event

cache.on('set', ({key, value}) => console.log(key, value))

Del event

cache.on('del', key => console.log(key))

Expire event

Trigged when key expires, by a TTL, LRU, LFU...

cache.on('expired', key => console.log(key))

Pooling Caches

You can store all your caches in one variable, recommended in big projects to organize code

Create new pool

import { CachePool } from 'cachetools-js'

const pool = new CachePool({maxsize: 10})

Create a cache in pool

//create a new TTLCache in pool, this function return the new cache
pool.createCache('c1', 'ttl', {ttl: 10})

Get a cache in pool

const cache = pool.getCache('c1')

Del a cache in pool

pool.delCache('c1')

Del all caches in pool

pool.delAllCaches()

Set a key in some cache in pool

pool.createCache('c1', 'ttl', {ttl: 10})

pool.set('c1', 'key', 'value')

Get a key in some cache in pool

pool.createCache('c1', 'ttl', {ttl: 10})

const cache = pool.get('c1', 'key')

Del a key in some cache in pool

pool.createCache('c1', 'ttl', {ttl: 10})

pool.del('c1', 'key')

Del all keys in some cache in pool

pool.createCache('c1', 'ttl', {ttl: 10})

pool.delAll('c1')

Get the length of pool

pool.createCache('c1', 'ttl', {ttl: 10})

const length = pool.length()

Decorator

You can decore a function to apply caching if you use same args

How to Use

import { cacheDecorator, TTLCache } from 'cachetools-js'

//function to sum values
const fn = (...args: number[]) => args.reduce((p, c) => p + c)
const cache = new TTLCache({ttl: 1000})
const decoredFn = cacheDecorator(fn, cache)

//will store result in cache and return
await decoredFn(1, 2, 3)

//will return cached value -- second call
await decoredFn(1, 2, 3)

//if you change args, the process will repeat