1.0.0 • Published 5 years ago
imc v1.0.0
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 --saveYarn:
$ yarn add imcCDN:
<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'); // undefinedTo 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 testRun tests in watch mode:
$ npm run test:watchLint files:
$ npm run lintFix lint errors:
$ npm run lint:fixRelease
Only collaborators with credentials can release and publish:
$ npm run release
$ git push --follow-tags && npm publish