1.0.9 • Published 2 years ago

easy-js-cache v1.0.9

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

Javascript Easy Cache

easy-js-cache will allow you to cache data locally. It is very convenient to save data from the API so as not to repeat requests.


Package in development process


installation

npm install easy-js-cache

Feature list:

  • Cache any type of data
  • Lifetime of cache
  • Half types
  • Panel to visualize cache

How to Use?

Method List:

  • configure(options)
  • set(key, value, params)
  • get(key)
  • has(key)
  • remove(key)
  • destroy()
  • keys()
  • on(type, cb)

Types:

ParamTypeRequiredDefaultDescription
keyStringtruenullKey for cached value
valueAnytruenullCaching value
params.expireInNumberoptionalnullDefault lifetime for current cache
options.defaultExpireInNumberoptional300000Default cache lifetime
options.withCacheToolsBooleanoptionalfalseShow panel with all cache
typeString ('get', 'set','destroy' or 'remove')truenullEvent type

Coding Example

Basic usage

import { cacheMachine } from 'js-easy-cache'

interface Client {
  readonly firstName: string,
  readonly lastName: string,
  readonly phone: string
}

const clientService = {

  getAllClients: () => {
    const key = 'allClients'
  
    //Checking if the cache exists
    if (cacheMachine.has(key)) {
      //Return cache if exist
      return cacheMachine.get<Client[]>(key)
    } else {
      //Make request
      const data = userService.getAll()
      //Set cache with 1 min lifetime
      cacheMachine.set(key, data, {
        expireIn: 60000
      })
    }
  },

  getOneClient: (uuid: string) => {
    const key = uuid
    if (cacheMachine.has(key)) {
      return cacheMachine.get<Client>(key)
    } else {
      return userService.get({ uuid })
    }
  }

}

Configure

//index.ts
import { cacheMachine } from 'js-easy-cache'
import 'easy-js-cache/dist/css/index.css'

cacheMachine.configure({
  withCacheTools: true,
  defaultExpireIn: 120000
})

...

In Progress

  • ⬜ Promises Method
  • ✅ More events
  • ⬜ Visualise events
  • ⬜ Optimize methods
  • ⬜ Sync with Browser Storage
  • ⬜ Better cache visualise

License

MIT


Good coding and have fun

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