It's core concept is to stay simple, small and easily extendable using plugins.
- write handlers for hook'able methods
- extend API of cache object
stash-it can be used in various environments (client, server or native), depending on what adapter you use.
It's very small ~2kB (including memory adapter, minified + gzipped) with no dependencies whatsoever.
At one time, I was looking for a cache mechanism for node, that would allow me to add tags to stored items. I found some solutions. But when I dug deeper I started to find various modules that were either too big, had too few / many methods, were hard to use or not maintained for a very long time.
Then I thought - if there isn't anything close to what I am looking for, why not create something of my own.
That's how stash-it came to be.
npm install stash-it --save
stash-it is just a core module, which provides means to create cache or register plugins. It doesn't come with any adapter or plugin out of the box. Therefore you will either need to provide an adapter or install one:
npm install stash-it-adapter-memory --save
Now, you have everything for the most basic usage of stash-it.
(mind that I am using ES6 syntax)
import { createCache } from 'stash-it';
import createMemoryAdapter from 'stash-it-adapter-memory';
// First, we need to create an adapter
const adapter = createMemoryAdapter();
// Now, let's create cache instance
const cache = createCache(adapter);
// Cool! Now time for some actions
cache.setItem('key', 'some very often fetched value I need to store');
cache.hasItem('key'); // true
const item = cache.getItem('key');
console.log(item.value); // some very often fetched value I need to store
cache.removeItem('key'); // true
cache.hasItem('key'); // false
And that's pretty much it.
stash-it is build in ES6 for modern environments. If you need to run it in older ones,
you will have to transpile it. See .babelrc
file for more details.
https://stash-it.gitbook.io/stash-it/
(for v2 head over here.)
- Dawid Młynarz for creating stash-it's logo;
MIT