IMC
IMC is an In-Memory Cache key-value store.
import cache from 'imc';
cache.set('key', 'value');
cache.get('key'); // 'value'
Installation
NPM:
$ npm install imc --save
Yarn:
$ yarn add imc
CDN:
<script src="https://unpkg.com/imc@latest/umd/imc.min.js"></script>
<script>
var cache = window.IMC.cache;
var Cache = window.IMC.Cache;
</script>
Usage
Import Module
// CommonJS
const { Cache, cache } = require('imc');
// ES Modules
import cache, { Cache } from 'imc';
Global Cache
You can either use the global cache store:
// cache is also a default export
import { cache } from 'imc';
Local Cache
Or create a local cache store:
import { Cache } from 'imc';
const cache = new Cache();
When instantiating a new cache store, the initial state can be set:
const cache = new Cache({ key: 'value' });
Set
Set key-value:
cache.set('key', 'value');
Set key-value using object:
cache.set({ key: 'value' });
Set multiple keys and values, which are merged to the store object:
cache.set({
key: 'value',
answer: 42,
});
Get
Get value from key:
cache.get('key'); // 'value'
If key-value does not exist, it will return undefined
:
cache.get('invalid'); // undefined
To differentiate from a key-value that hasn't be set, you can use null
:
cache.set('invalid', null);
Delete
Delete key-value from store:
cache.delete('key');
Clear
Clear store:
cache.clear();
This means the store becomes an empty object:
cache.get(); // {}
Testing
Run tests with coverage:
$ npm test
Run tests in watch mode:
$ npm run test:watch
Lint files:
$ npm run lint
Fix lint errors:
$ npm run lint:fix
Release
Only collaborators with credentials can release and publish:
$ npm run release
$ git push --follow-tags && npm publish