1.0.3 • Published 6 years ago

poormans-cache v1.0.3

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

poormans-cache Build Status

A simple, persistent cache for dead-simple apps.

Install

$ yarn add poormans-cache
# or npm i poormans-cache

Usage

import Cache from 'poormans-cache'
// or const Cache = require('poormans-cache')

// Cache(<Boolean saveFile>, <String filename>)
// e.g const cache = Cache()
// or const cache = require('poormans-cache')()
const cache = Cache(true, './cache.json')

cache.set('hello', 'there')
cache.get('hello') // there

cache.del('hello')
cache.get('hello') // undefined

cache.set('🌚', '🌝', 2) // save for 2 seconds
cache.get('🌚', true) // { val: 🌝, expires: 1535065557820 }

setTimeout(() => cache.get('🌚'), 1000) // 🌝 
setTimeout(() => cache.get('🌚'), 1000) // undefined 

API

Note: All methods are synchronous.

Cache.set(key, value)

Returns an object: { val: <value>, expires: <ms> }.

  • key: string
  • value: string | any serializable object

Cache.get(key, showExpire)

Returns the entry value.

When showExpire is true, it returns an object: { val: <value>, expires: <ms> }.

  • key: string
  • showExpire: boolean

Cache.del(key)

  • key: string

Test

$ yarn test

License

MIT

1.0.3

6 years ago

1.0.2

6 years ago