1.2.8 • Published 11 months ago

js-simple-cache v1.2.8

Weekly downloads
-
License
ISC
Repository
github
Last release
11 months ago

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()
1.2.8

11 months ago

1.2.7

11 months ago

1.2.6

11 months ago

1.2.5

11 months ago

1.2.4

11 months ago

1.2.3

12 months ago

1.2.2

12 months ago

1.2.1

12 months ago

1.2.0

12 months ago

1.1.0

12 months ago

1.0.10

12 months ago

1.0.9

12 months ago

1.0.8

12 months ago

1.0.7

12 months ago

1.0.6

12 months ago

1.0.5

12 months ago

1.0.4

12 months ago

1.0.3

12 months ago

1.0.2

12 months ago

1.0.1

12 months ago

1.1.41

2 years ago

1.1.40

2 years ago

1.1.39

2 years ago

1.1.38

2 years ago

1.1.37

2 years ago

1.1.36

2 years ago

1.1.35

2 years ago

1.1.34

2 years ago

1.1.33

2 years ago

1.1.32

2 years ago

1.1.31

2 years ago

1.1.30

2 years ago

1.1.29

2 years ago

1.1.28

2 years ago

1.1.27

2 years ago

1.1.26

2 years ago

1.1.25

2 years ago

1.1.24

2 years ago

1.1.23

2 years ago

1.1.22

2 years ago

1.1.21

2 years ago

1.1.20

2 years ago

1.1.19

2 years ago

1.1.18

2 years ago

1.1.17

2 years ago

1.1.16

2 years ago

1.1.15

2 years ago

1.1.14

2 years ago

1.1.13

2 years ago

1.1.12

2 years ago

1.1.11

2 years ago

1.1.10

2 years ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.0.0

2 years ago