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

js-simple-cache

v1.2.8

Published

"A simple caching object data base on ES6 Map() that support auto invalidate cache when reach maximum size

Downloads

7

Readme

js-simple-cache

A simple caching object data base on ES6 Map() that support auto invalidate cache when reach maximum size. This'll work if all object you wanna cache have a unique key like when you cache user data, all object have a key like userID

Install

npm i js-simple-cache

Usage

// v1.0.7
const Cache = require('js-simple-cache')
const cache = new Cache('userID', 10000)
// v1.2.0
// Node module
const { Cache } = require('js-simple-cache')
const cache = new Cache('userID', 10000)
// ES module
import { Cache } from 'js-simple-cache'
const cache = new Cache('userID', 10000)

options

  • First aggrument is unique key, when you set new item, it auto set with that key for you and you can access later with get
  • Second arrgrument is limit size, it help cache to auto delete least use when cache reach maximum size limit (but you can set undefined to have no limit)
  • Note: This only accept cache object has a props that you provide when init

API provide

Getter and Setter

  • Getter only: key, size
  • Getter and Setter: limit (Integer and greater than current size)

Store an item

  • First aggrument is the object that contain key, second aggrument is a timer (miliseconds) that will help you auto remove item after X miliseconds.
cache.set({userID:1, username: 'HOAI AN'}, 10000) // item will auto remove after 10 seconds
  • This is base on ES6 Map so if you put same key it gonna replace old item

Get an item

const user = cache.get(1)
console.log(user) // {userID:1, username: 'HOAI AN'}
  • If you using typescript and you cache object create by a class, you can casting like by using as like this.
const order = cache.get(110230) as Order // Order is your class

Remove an item

cache.remove(1)

Clear cache

cache.clear()

find a key

cache.set({ userID: 1, username: 'HOAI AN' })
cache.set({ userID: 2, username: 'HOAI AN1' })
cache.set({ userID: 3, username: 'HOAI AN2' })
cache.set({ userID: 4, username: 'HOAI AN3' })
cache.set({ userID: 5, username: 'HOAI AN4' })
cache.set({ userID: 6, username: 'HOAI AN5' })
const key = cache.findKey(item => item.username === 'HOAI AN3')
console.log(key) // 4

check that cache have a key

cache.has(2)

filter

const key = cache.filter(item => item.username.includes('HOAIAN'))
  • You can convert to Object / Array to use other prototype

search

const options = {
    searchValue: 'string',
    searchFields: ['field1', 'field2'], // specified object field to search ( if you have nested object or array, enable deepScan)
    nocase: true, // optional set true to match whatever 
    deepScan: true // optional set true to enable nested object scan
}
const result = cache.search(options)

Export to Object / Array

cache.toArray()
cache.toObject()
  • If you export to Object, it gonna use your key values when you create cache as key for Object

Export to JSON (Object / Array)

cache.toJSONArray()
cache.toJSONObject()