Skip to content

smolak/stash-it-adapter-memory

Repository files navigation

logo-stash-it-color-dark 2x

stash-it-adapter-memory

build status Coverage Status

Memory adapter for stash-it.

It's build in ES6 so if you need to run it in an older environment, you will need to transpile it.

Installation

npm i stash-it-adapter-memory --save

Usage

import { createCache } from 'stash-it';
import createMemoryAdapter from 'stash-it-adapter-memory';

const adapter = createMemoryAdapter();
const cache = createCache(adapter);

And that's it. You are ready to go.

For available methods, check adapters API section (all adapters have the same API).

Heads-up!

Any instance of cache will have access to all items stored in memory, regardles of which cache instance was used. This is because all of the adapters are to behave in the same manner, e.g. connecting to the very same instance of redis / mongo / ... will grant access to the very same items stored there. So should this adapter.

Have a look:

// file1.js - executed BEFORE
import { createCache } from 'stash-it';
import createMemoryAdapter from 'stash-it-adapter-memory';

const adapter = createMemoryAdapter();
const cache1 = createCache(adapter);

cache1.setItem('key', 'value');


// file2.js - executed AFTER
import { createCache } from 'stash-it';
import createMemoryAdapter from 'stash-it-adapter-memory';

const adapter = createMemoryAdapter();
const cache2 = createCache(adapter);

cache2.hasItem('key'); // true

And that goes for all of the methods.

How to bypass this (if needed)?

The suggested way is to use a prefix / suffix plugin.