@consenlabs-fe/memory-cache
v0.1.0
Published
key-value pair cache tool.
Downloads
185
Readme
Memory Cache
key-value pair cache tool.
Features
- Multiple environment support
- Stored in memory only
- Extremely small size (<1kb)
- No side effects
Compatibility
- Browser: FF 45+, Safari 9+, Chrome 42+
- Browser (low version): Add
class
polyfill from Babel or core-js - NodeJS: Full support
- React Native: Full support
Guide
Install
- Run
npm i @consenlabs-fe/memory-cache
. - ESM:
import MemoryCache from '@consenlabs-fe/memory-cache'
- CJS (NodeJS):
const MemoryCache require('@consenlabs-fe/memory-cache')
Examples
1. Get and Set
const cache = new MemoryCache('my-caches')
cache.setItem('name', 'witt')
cache.getItem('name') // -> 'witt'
2. Remove and Clean
const cache = new MemoryCache('my-caches')
cache.setItem('name', 'witt')
cache.setItem('location', 'china')
cache.removeItem('name')
cache.getItem('name') // -> null
cache.getItem('location') // -> 'china'
cache.cleanAll()
cache.getItem('location') // -> null
3. Expiration
const cache = new MemoryCache('my-caches', { maxAge: 100 })
cache.setItem('name', 'witt')
cache.getItem('name') // -> 'witt'
setTimeout(() => {
cache.getItem('name') // -> null
}, 200)