1.2.8 • Published 2 years ago

js-simple-cache v1.2.8

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years 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

2 years ago

1.2.7

2 years ago

1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.1.41

3 years ago

1.1.40

3 years ago

1.1.39

3 years ago

1.1.38

3 years ago

1.1.37

3 years ago

1.1.36

3 years ago

1.1.35

3 years ago

1.1.34

3 years ago

1.1.33

3 years ago

1.1.32

3 years ago

1.1.31

3 years ago

1.1.30

3 years ago

1.1.29

3 years ago

1.1.28

3 years ago

1.1.27

3 years ago

1.1.26

3 years ago

1.1.25

3 years ago

1.1.24

3 years ago

1.1.23

3 years ago

1.1.22

3 years ago

1.1.21

3 years ago

1.1.20

3 years ago

1.1.19

3 years ago

1.1.18

3 years ago

1.1.17

3 years ago

1.1.16

3 years ago

1.1.15

3 years ago

1.1.14

3 years ago

1.1.13

3 years ago

1.1.12

3 years ago

1.1.11

3 years ago

1.1.10

3 years ago

1.1.9

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.0.0

3 years ago