stash-it-plugin-readonly
v1.0.1
Published
Read only plugin for stash-it
Downloads
3
Readme
stash-it-plugin-readOnly
Read-only plugin for stash-it.
The problem
Let's say you need to have an instance of stash-it that is only capable of reading from the storage, but not writing, changing or deleting anything.
Solution
This plugin will allow you to do just that. And if anyone will try to write / delete something, an error will be thrown.
Installation
npm i stash-it-plugin-readonly --save
Usage
import { createCache } from 'stash-it';
import createReadOnlyPlugin from 'stash-it-plugin-readonly';
// You can use any adapter
import createMemoryAdapter from 'stash-it-adapter-memory';
const cacheInstance = createCache(createMemoryAdapter());
const readOnlyPlugin = createReadOnlyPlugin();
const readOnlyCacheInstance = cacheInstance.registerPlugins([ readOnlyPlugin ]);
// This will work
readOnlyCacheInstance.hasItem('key');
// But this will throw
readOnlyCacheInstance.removeItem('key');
// or this
readOnlyCacheInstance.setItem('key', 'value');
Methods that will throw are: setItem, addExtra, setExtra and removeItem. All other will work just fine.