Home

Awesome

cache-base NPM version NPM monthly downloads NPM total downloads Linux Build Status

Basic object cache with get, set, del, and has methods for node.js/javascript projects.

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.

(TOC generated by verb using markdown-toc)

Install

Install with npm:

$ npm install --save cache-base

Quickstart

const CacheBase = require('cache-base');
const app = new CacheBase();

app.set('a.b', 'c');

console.log(app.cache.a);    //=> { b: 'c' }
console.log(app.cache.a.b);  //=> 'c'

console.log(app.get('a'));   //=> { b: 'c' }
console.log(app.get('a.b')); //=> 'c'

More usage examples below.

API

Params

Example

const app = new CacheBase();

.set

Assign value to key. Also emits set with the key and value.

Params

Events

Example

app.on('set', function(key, val) {
  // do something when `set` is emitted
});

app.set('admin', true);

// also takes an object or an array of objects
app.set({ name: 'Brian' });
app.set([{ foo: 'bar' }, { baz: 'quux' }]);
console.log(app);
//=> { name: 'Brian', foo: 'bar', baz: 'quux' }

.get

Return the value of key.

Params

Events

Example

app.set('a.b.c', 'd');
app.get('a.b');
//=> { c: 'd' }

.prime

Create a property on the cache with the given value only if it doesn't already exist.

Params

Example

console.log(app.cache); //=> {}
app.set('one', { foo: 'bar' });
app.prime('one', { a: 'b' });
app.prime('two', { c: 'd' });
console.log(app.cache.one); //=> { foo: 'bar' }
console.log(app.cache.two); //=> { c: 'd' }

.default

Set a default value to be used when .get() is called and the value is not defined on the cache. Returns a value from the defaults when only a key is passed.

Params

Example

app.set('foo', 'xxx');
app.default('foo', 'one');
app.default('bar', 'two');
app.default('baz', 'three');
app.set('baz', 'zzz');

console.log(app.get('foo'));
//=> 'xxx'

console.log(app.get('bar'));
//=> 'two'

console.log(app.get('baz'));
//=> 'zzz'

console.log(app);
// CacheBase {
//   cache: { foo: 'xxx', bar: 'two', baz: 'zzz' },
//   defaults: { foo: 'one', bar: 'two', baz: 'three' } }

.union

Set an array of unique values on cache key.

Params

Example

app.union('a.b.c', 'foo');
app.union('a.b.c', 'bar');
app.union('a.b.c', ['bar', 'baz']);
console.log(app.get('a'));
//=> { b: { c: ['foo', 'bar', 'baz'] } }

.has

Return true if the value of property key is not undefined.

Params

Example

app.set('foo', true);
app.set('baz', null);
app.set('bar', undefined);

app.has('foo'); //=> true
app.has('bar'); //=> true
app.has('baz'); //=> false

.hasOwn

Returns true if the specified property is an own (not inherited) property. Similar to .has(), but returns true if the key exists, even if the value is undefined.

Params

Example

app.set('a.b.c', 'd');
app.set('x', false);
app.set('y', null);
app.set('z', undefined);

app.hasOwn('a');      //=> true
app.hasOwn('b');      //=> true
app.hasOwn('c');      //=> true
app.hasOwn('a.b.c');  //=> true
app.hasOwn('x');      //=> true
app.hasOwn('y');      //=> true
app.hasOwn('z');      //=> true
app.hasOwn('lslsls'); //=> false

.del

Delete one or more properties from the instance.

Params

Events

Example

// setup a listener to update a property with a default
// value when it's deleted by the user
app.on('del', key => app.set(key, app.default(key)));

app.del(); // delete all properties on the cache
// or
app.del('foo');
// or an array of keys
app.del(['foo', 'bar']);

.clear

Reset the entire cache to an empty object. Note that this does not also clear the defaults object, since you can manually do cache.defaults = {} if you want to reset that object as well.

Example

// clear "defaults" whenever the cache is cleared
app.on('clear', key => (app.defaults = {}));
app.clear();

.visit

Visit (or map visit) the specified method (key) over the properties in the given object or array.

Params

.keys

Gets an array of names of all enumerable properties on the cache.

Example

const app = new CacheBase();
app.set('user', true);
app.set('admin', false);

console.log(app.keys);
//=> ['user', 'admin']

.size

Gets the length of keys.

Example

const app = new CacheBase();
app.set('user', true);
app.set('admin', false);

console.log(app.size);
//=> 2

Usage examples

Create an instance of cache-base

const app = new CacheBase();

app.set('a', 'b');
app.set('c.d', 'e');

console.log(app.get('a'));
//=> 'b'
console.log(app.get('c'));
//=> { d: 'e' }
console.log(app);
//=> CacheBase { a: 'b' }

Initialize with an object

const app = new CacheBase({ a: 'b', c: { d: 'e' } });

console.log(app.get('a'));
//=> 'b'
console.log(app.get('c'));
//=> { d: 'e' }
console.log(app.get('c.d'));
//=> 'e'
console.log(app);
//=> CacheBase { cache: { a: 'b' } }

Inherit

class MyApp extends CacheBase {}

const app = new MyApp();
app.set('a', 'b');
app.set('c', 'd');

console.log(app.get('a'));
//=> 'b'

console.log(app);
//=> MyApp { cache: { a: 'b', c: 'd' } }

Custom namespace

Pass a string as the first value to the contructor to define a custom property name to use for the cache. By default values are stored on the cache property.

const CacheBase = require('cache-base');
const app = new CacheBase('data', { a: 'b' });
app.set('c.d', 'e');

// get values
console.log(app.get('a'));
//=> 'b'
console.log(app.get('c'));
//=> { d: 'e' }
console.log(app.data);
//=> { a: 'b', c: { d: 'e' } }
console.log(app);
//=> CacheBase { data: { a: 'b', c: { d: 'e' } } }

About

<details> <summary><strong>Contributing</strong></summary>

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

</details> <details> <summary><strong>Running Tests</strong></summary>

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test
</details> <details> <summary><strong>Building docs</strong></summary>

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb
</details>

Related projects

You might also be interested in these projects:

Contributors

CommitsContributor
67jonschlinkert
2wtgtybhertgeghgtwtg

Author

Jon Schlinkert

License

Copyright © 2018, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.6.0, on March 23, 2018.